This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Start := "ab" A | |
| "aba" | |
A := "a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Foo := Foo "ab" | |
| "ab" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def my_rule | |
if my_rule # infinite loop here | |
do something | |
else | |
do something else | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assign := Identifier EQUALS Number | |
Identifier := WORD | |
Number := NUMBER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assign := Identifier EQUALS Number | |
:= WORD EQUALS NUMBER | |
:= foo EQUALS NUMBER # foo is a valid workd token, we can replace it | |
:= foo = NUMBER # = is a valid equals token | |
:= foo = 1 # 1 is a valid number token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[PARAGRAPH] | |
| | |
v | |
+-------[SENTENCES]-----------+ | |
| | | | |
v v v | |
[TEXT="hello "] [BOLD="world"] [TEXT="."] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parser.new.parse("__Foo__ and *bar*.\n\nAnother paragraph.") | |
=> #<BodyNode:0x007fc774abe008 | |
@consumed=14, | |
@paragraphs= | |
[#<ParagraphNode:0x007fc774eb25d8 | |
@consumed=12, | |
@sentences= | |
[#<Node:0x007fc774ea8150 @consumed=5, @type="BOLD", @value="Foo">, | |
#<Node:0x007fc774ac5f60 @consumed=1, @type="TEXT", @value=" and ">, | |
#<Node:0x007fc774ac6758 @consumed=3, @type="EMPHASIS", @value="bar">, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parser | |
def parse(tokens) | |
body = body_parser.match(tokens) | |
raise "Syntax error: #{tokens[body.consumed]}" unless tokens.count == body.consumed | |
body | |
end | |
private | |
def body_parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BodyParser < BaseParser | |
include MatchesStar | |
def match(tokens) | |
nodes, consumed = match_star tokens, with: paragraph_parser | |
return Node.null if nodes.empty? | |
BodyNode.new(paragraphs: nodes, consumed: consumed) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SentencesAndNewlineParser < BaseParser | |
include MatchesStar | |
def match(tokens) | |
nodes, consumed = match_star tokens, with: sentence_parser | |
return Node.null if nodes.empty? | |
return Node.null unless tokens.peek_at(consumed, 'NEWLINE', 'NEWLINE') | |
consumed += 2 # consume newlines | |
ParagraphNode.new(sentences: nodes, consumed: consumed) |