mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
Implemented usingbygroup action, so code-in-markdown works
This commit is contained in:
parent
a2884c4c78
commit
3d5d073471
2
TODO.md
2
TODO.md
@ -9,4 +9,4 @@
|
|||||||
* ✅ Implement lexer loader by file extension
|
* ✅ Implement lexer loader by file extension
|
||||||
* ✅ Add --line-numbers to terminal formatter
|
* ✅ Add --line-numbers to terminal formatter
|
||||||
* Implement lexer loader by mime type
|
* Implement lexer loader by mime type
|
||||||
* Implement Delegating lexers
|
* ✅ Implement Delegating lexers
|
||||||
|
@ -18,19 +18,21 @@
|
|||||||
<rule pattern="^(\s*)([*-])(\s)(.+\n)"><bygroups><token type="TextWhitespace"/><token type="Keyword"/><token type="TextWhitespace"/><usingself state="inline"/></bygroups></rule>
|
<rule pattern="^(\s*)([*-])(\s)(.+\n)"><bygroups><token type="TextWhitespace"/><token type="Keyword"/><token type="TextWhitespace"/><usingself state="inline"/></bygroups></rule>
|
||||||
<rule pattern="^(\s*)([0-9]+\.)( .+\n)"><bygroups><token type="TextWhitespace"/><token type="Keyword"/><usingself state="inline"/></bygroups></rule>
|
<rule pattern="^(\s*)([0-9]+\.)( .+\n)"><bygroups><token type="TextWhitespace"/><token type="Keyword"/><usingself state="inline"/></bygroups></rule>
|
||||||
<rule pattern="^(\s*>\s)(.+\n)"><bygroups><token type="Keyword"/><token type="GenericEmph"/></bygroups></rule>
|
<rule pattern="^(\s*>\s)(.+\n)"><bygroups><token type="Keyword"/><token type="GenericEmph"/></bygroups></rule>
|
||||||
<rule pattern="^(\s*```\n[\w\W]*?^\s*```$\n)"><token type="LiteralStringBacktick"/></rule>
|
<rule pattern="^(```\n)([\w\W]*?)(^```$)">
|
||||||
<rule pattern="(?x)
|
<bygroups>
|
||||||
^(?P<initial>\s*```)
|
<token type="LiteralStringBacktick"/>
|
||||||
(?P<lang>[\w\-]+)
|
|
||||||
(?P<afterlang>
|
|
||||||
(?P<whitespace>[^\S\n]+)
|
|
||||||
(?P<extra>.*))?
|
|
||||||
(?P<newline>\n)
|
|
||||||
(?P<code>(.|\n)*?)
|
|
||||||
(?P<terminator>^\s*```$\n)
|
|
||||||
">
|
|
||||||
<!-- Implement actions for delegating via a capture group -->
|
|
||||||
<token type="Text"/>
|
<token type="Text"/>
|
||||||
|
<token type="LiteralStringBacktick"/>
|
||||||
|
</bygroups>
|
||||||
|
</rule>
|
||||||
|
<rule pattern="^(```)(\w+)(\n)([\w\W]*?)(^```$)">
|
||||||
|
<bygroups>
|
||||||
|
<token type="LiteralStringBacktick"/>
|
||||||
|
<token type="NameLabel"/>
|
||||||
|
<token type="TextWhitespace"/>
|
||||||
|
<UsingByGroup lexer="2" content="4"/>
|
||||||
|
<token type="LiteralStringBacktick"/>
|
||||||
|
</bygroups>
|
||||||
</rule>
|
</rule>
|
||||||
<rule><include state="inline"/></rule>
|
<rule><include state="inline"/></rule>
|
||||||
</state>
|
</state>
|
||||||
|
@ -16,13 +16,16 @@ module Tartrazine
|
|||||||
Push
|
Push
|
||||||
Token
|
Token
|
||||||
Using
|
Using
|
||||||
|
Usingbygroup
|
||||||
Usingself
|
Usingself
|
||||||
end
|
end
|
||||||
|
|
||||||
struct Action
|
struct Action
|
||||||
property actions : Array(Action) = [] of Action
|
property actions : Array(Action) = [] of Action
|
||||||
|
|
||||||
|
@content_index : Int32 = 0
|
||||||
@depth : Int32 = 0
|
@depth : Int32 = 0
|
||||||
|
@lexer_index : Int32 = 0
|
||||||
@lexer_name : String = ""
|
@lexer_name : String = ""
|
||||||
@states : Array(String) = [] of String
|
@states : Array(String) = [] of String
|
||||||
@states_to_push : Array(String) = [] of String
|
@states_to_push : Array(String) = [] of String
|
||||||
@ -62,6 +65,9 @@ module Tartrazine
|
|||||||
@states = xml.attributes.select { |attrib|
|
@states = xml.attributes.select { |attrib|
|
||||||
attrib.name == "state"
|
attrib.name == "state"
|
||||||
}.map &.content
|
}.map &.content
|
||||||
|
when ActionType::Usingbygroup
|
||||||
|
@lexer_index = xml["lexer"].to_i
|
||||||
|
@content_index = xml["content"].to_i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -134,6 +140,12 @@ module Tartrazine
|
|||||||
tokenizer.lexer.states[new_state.name] = new_state
|
tokenizer.lexer.states[new_state.name] = new_state
|
||||||
tokenizer.state_stack << new_state.name
|
tokenizer.state_stack << new_state.name
|
||||||
[] of Token
|
[] of Token
|
||||||
|
when ActionType::Usingbygroup
|
||||||
|
# Shunt to content-specified lexer
|
||||||
|
return [] of Token if match.empty?
|
||||||
|
Tartrazine.lexer(String.new(match[@lexer_index].value)).tokenizer(
|
||||||
|
String.new(match[@content_index].value),
|
||||||
|
secondary: true).to_a
|
||||||
else
|
else
|
||||||
raise Exception.new("Unknown action type: #{@type}")
|
raise Exception.new("Unknown action type: #{@type}")
|
||||||
end
|
end
|
||||||
|
@ -48,6 +48,7 @@ module Tartrazine
|
|||||||
root_lexer = lexer(root)
|
root_lexer = lexer(root)
|
||||||
DelegatingLexer.new(language_lexer, root_lexer)
|
DelegatingLexer.new(language_lexer, root_lexer)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return a list of all lexers
|
# Return a list of all lexers
|
||||||
def self.lexers : Array(String)
|
def self.lexers : Array(String)
|
||||||
LEXERS_BY_NAME.keys.sort!
|
LEXERS_BY_NAME.keys.sort!
|
||||||
|
Loading…
Reference in New Issue
Block a user