Class: Antlers::Lexer
- Inherits:
-
Object
- Object
- Antlers::Lexer
- Defined in:
- lib/lexer.rb
Constant Summary collapse
- FOR_KEYWORDS =
['for:', 'in:', ':for'].freeze
- FORM_KEYWORDS =
['form:', ':form'].freeze
Instance Method Summary collapse
-
#initialize ⇒ Lexer
constructor
A new instance of Lexer.
- #parse(template) ⇒ Object
Constructor Details
#initialize ⇒ Lexer
Returns a new instance of Lexer.
14 15 16 17 18 |
# File 'lib/lexer.rb', line 14 def initialize @delimiters = ['<{', '}>', '{', '}'] @keywords = ['if:', *FORM_KEYWORDS, *FOR_KEYWORDS, 'slot:', ':slot'] @cursor = 0 end |
Instance Method Details
#parse(template) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lexer.rb', line 20 def parse(template) @cursor = 0 sequence = [] # Split on delimiters and retain capture groups. segments = template.split(/(#{Regexp.union(@delimiters)})/).map(&:strip) until segments[@cursor].nil? if (antlers_segment = antlers_segment(segments:)) sequence << antlers_lexeme(antlers_segment:, segments:) # Skipping: ['{', 'expression', '}'] # Skipping: ['<{', 'name + props + keywords', '}>'] @cursor += 3 else segment = segments[@cursor] sequence << segment unless segment.empty? @cursor += 1 end end sequence end |