Class: Luoma::BaseLexer
- Inherits:
-
Object
- Object
- Luoma::BaseLexer
- Defined in:
- lib/luoma/lexer.rb,
sig/luoma/lexer.rbs
Overview
The base class for template source code tokenizers.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#tokens ⇒ Array[t_token]
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
-
#emit(kind) ⇒ void
(t_token_kind) -> void.
- #go ⇒ void
-
#index(pattern) ⇒ Integer?
(Regexp) -> Integer?.
-
#initialize(env, source) ⇒ BaseLexer
constructor
A new instance of BaseLexer.
-
#scan(pattern) ⇒ String?
(Regexp) -> String?.
-
#scan_markup ⇒ Symbol?
() -> Symbol?.
-
#scan_until?(pattern) ⇒ Boolean
(Regexp) -> bool.
-
#skip?(pattern) ⇒ Boolean
(Regexp) -> bool.
-
#skip_until?(pattern) ⇒ Boolean
(Regexp) -> bool.
Constructor Details
#initialize(env, source) ⇒ BaseLexer
Returns a new instance of BaseLexer.
18 19 20 21 22 23 24 |
# File 'lib/luoma/lexer.rb', line 18 def initialize(env, source) @env = env @source = source @scanner = StringScanner.new(source) @start = 0 @tokens = [] #: Array[t_token] end |
Instance Attribute Details
#tokens ⇒ Array[t_token] (readonly)
Returns the value of attribute tokens.
8 9 10 |
# File 'lib/luoma/lexer.rb', line 8 def tokens @tokens end |
Class Method Details
.tokenize(env, source) ⇒ Array[t_token]
11 12 13 14 15 |
# File 'lib/luoma/lexer.rb', line 11 def self.tokenize(env, source) lexer = new(env, source) lexer.go lexer.tokens end |
Instance Method Details
#emit(kind) ⇒ void
This method returns an undefined value.
(t_token_kind) -> void
44 45 46 47 48 |
# File 'lib/luoma/lexer.rb', line 44 def emit(kind) # @tokens << { kind: kind, start: @start, stop: @scanner.pos - 1 } @tokens << [kind, @start, @scanner.pos - 1] @start = @scanner.pos end |
#go ⇒ void
This method returns an undefined value.
27 28 29 30 |
# File 'lib/luoma/lexer.rb', line 27 def go state = :scan_markup state = send(state) until state.nil? end |
#index(pattern) ⇒ Integer?
(Regexp) -> Integer?
57 58 59 60 61 62 |
# File 'lib/luoma/lexer.rb', line 57 def index(pattern) byte_offset = @scanner.exist?(pattern) return nil unless byte_offset @scanner.pos + byte_offset - (@scanner.matched_size || raise) end |
#scan(pattern) ⇒ String?
(Regexp) -> String?
65 66 67 68 69 |
# File 'lib/luoma/lexer.rb', line 65 def scan(pattern) # NOTE: calling @scanner.scan directly yields a significant increase in # performance @scanner.scan(pattern) end |
#scan_markup ⇒ Symbol?
() -> Symbol?
38 39 40 |
# File 'lib/luoma/lexer.rb', line 38 def scan_markup raise "not implemented" end |
#scan_until?(pattern) ⇒ Boolean
(Regexp) -> bool
73 74 75 76 77 78 79 80 81 |
# File 'lib/luoma/lexer.rb', line 73 def scan_until?(pattern) byte_offset = @scanner.exist?(pattern) if byte_offset.nil? false else @scanner.pos += byte_offset - (@scanner.matched_size || raise) true end end |
#skip?(pattern) ⇒ Boolean
(Regexp) -> bool
84 85 86 87 88 89 90 91 |
# File 'lib/luoma/lexer.rb', line 84 def skip?(pattern) if @scanner.scan(pattern) @start = @scanner.pos true else false end end |
#skip_until?(pattern) ⇒ Boolean
(Regexp) -> bool
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/luoma/lexer.rb', line 95 def skip_until?(pattern) byte_offset = @scanner.exist?(pattern) if byte_offset.nil? false else @scanner.pos += byte_offset - (@scanner.matched_size || raise) @start = @scanner.pos true end end |