This file contains hidden or 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 SentenceParser < BaseParser | |
| def match(tokens) | |
| match_first tokens, emphasis_parser, bold_parser, text_parser | |
| end | |
| end |
This file contains hidden or 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 BoldParser < BaseParser | |
| def match(tokens) | |
| return Node.null unless tokens.peek_or(%w(UNDERSCORE UNDERSCORE TEXT UNDERSCORE UNDERSCORE), %w(STAR STAR TEXT STAR STAR)) | |
| Node.new(type: 'BOLD', value: tokens.third.value, consumed: 5) | |
| end | |
| end |
This file contains hidden or 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 TextParser < BaseParser | |
| def match(tokens) | |
| return Node.null unless tokens.peek('TEXT') | |
| Node.new(type: 'TEXT', value: tokens.first.value, consumed: 1) | |
| end | |
| end |
This file contains hidden or 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]--------+ | |
| | | | | |
| v v v | |
| [BINOP] [OPERATOR=-] [NUMBER=4] | |
| | | |
| +------------+----------+ | |
| | | | | |
| v v v | |
| [NUMBER=2] [OPERATOR=+] [NUMBER=2] |
This file contains hidden or 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]----------+ | |
| | | | | |
| v v v | |
| [NUMBER=2] [OPERATOR=+] [BINOP] | |
| | | |
| +------------+----------+ | |
| | | | | |
| v v v | |
| [NUMBER=2] [OPERATOR=-] [NUMBER=4] |
This file contains hidden or 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 := Binop | |
| Binop := Binop Operator Binop | |
| | Number | |
| Operator := + | - | * | / | |
| Number := 0 | 1 | 2 | ... | 9 |
This file contains hidden or 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 BaseParser | |
| private | |
| # We use some reflection to prettify the parser depedencies. Basically, from | |
| # calling a `foo_parser` method is the same as doing `ParserFactory.build('foo_parser')`. | |
| # | |
| def method_missing(name, *args, &block) | |
| raise ArgumentError.new("Method #{name} does not exist.") unless name.to_s.end_with?('_parser') | |
| ParserFactory.build(name, *args, &block) | |
| end |
This file contains hidden or 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 := Binop | |
| Binop := Subtraction | |
| Subtraction := Adition "-" Binop | |
| | Adition | |
| Adition := Division "+" Binop | |
| | Division | |
| Division := Multiplication "/" Binop | |
| | Multiplication | |
| Multiplication := Number "*" Binop | |
| | Number |
This file contains hidden or 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
| Body := Paragraph* | |
| Paragraph := SentenceAndNewline | |
| | SentenceAndEOF | |
| SentenceAndNewline := Sentence+ NEWLINE NEWLINE | |
| SentencesAndEOF := Sentence+ NEWLINE EOF | |
| | Sentence+ EOF | |
| Sentence := EmphasizedText |
This file contains hidden or 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 Generator | |
| def generate(ast) | |
| body_visitor.visit(ast) | |
| end | |
| private | |
| def body_visitor | |
| BodyVisitor.new | |
| end |