Class: Lrama::Lexer
- Inherits:
-
Object
- Object
- Lrama::Lexer
- Defined in:
- lib/lrama/lexer.rb,
lib/lrama/lexer/token.rb,
lib/lrama/lexer/location.rb,
lib/lrama/lexer/token/int.rb,
lib/lrama/lexer/token/str.rb,
lib/lrama/lexer/token/tag.rb,
lib/lrama/lexer/token/base.rb,
lib/lrama/lexer/token/char.rb,
lib/lrama/lexer/token/empty.rb,
lib/lrama/lexer/token/ident.rb,
lib/lrama/lexer/token/token.rb,
lib/lrama/lexer/grammar_file.rb,
lib/lrama/lexer/token/user_code.rb,
lib/lrama/lexer/token/instantiate_rule.rb
Defined Under Namespace
Modules: Token Classes: GrammarFile, Location
Constant Summary collapse
- SYMBOLS =
: Array
['%{', '%}', '%%', '{', '}', '\[', '\]', '\(', '\)', '\,', ':', '\|', ';'].freeze
- PERCENT_TOKENS =
%w( %union %token %type %nterm %left %right %nonassoc %expect %define %require %printer %destructor %lex-param %parse-param %initial-action %precedence %prec %error-token %before-reduce %after-reduce %after-shift-error-token %after-shift %after-pop-stack %empty %code %rule %no-stdlib %inline %locations %categories %start ).freeze
Instance Attribute Summary collapse
-
#end_symbol ⇒ Object
: String?.
-
#head_column ⇒ Object
readonly
: Integer.
-
#head_line ⇒ Object
readonly
@rbs!.
-
#line ⇒ Object
readonly
: Integer.
-
#status ⇒ Object
: :initial | :c_declaration.
Instance Method Summary collapse
- #column ⇒ Object
-
#initialize(grammar_file) ⇒ Lexer
constructor
A new instance of Lexer.
- #lex_c_code ⇒ Object
- #lex_token ⇒ Object
- #location ⇒ Object
- #next_token ⇒ Object
Constructor Details
#initialize(grammar_file) ⇒ Lexer
Returns a new instance of Lexer.
67 68 69 70 71 72 73 74 |
# File 'lib/lrama/lexer.rb', line 67 def initialize(grammar_file) @grammar_file = grammar_file @scanner = StringScanner.new(grammar_file.text) @head_column = @head = @scanner.pos @head_line = @line = 1 @status = :initial @end_symbol = nil end |
Instance Attribute Details
#end_symbol ⇒ Object
: String?
29 30 31 |
# File 'lib/lrama/lexer.rb', line 29 def end_symbol @end_symbol end |
#head_column ⇒ Object (readonly)
: Integer
26 27 28 |
# File 'lib/lrama/lexer.rb', line 26 def head_column @head_column end |
#head_line ⇒ Object (readonly)
25 26 27 |
# File 'lib/lrama/lexer.rb', line 25 def head_line @head_line end |
#line ⇒ Object (readonly)
: Integer
27 28 29 |
# File 'lib/lrama/lexer.rb', line 27 def line @line end |
#status ⇒ Object
: :initial | :c_declaration
28 29 30 |
# File 'lib/lrama/lexer.rb', line 28 def status @status end |
Instance Method Details
#column ⇒ Object
87 88 89 |
# File 'lib/lrama/lexer.rb', line 87 def column @scanner.pos - @head end |
#lex_c_code ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/lrama/lexer.rb', line 153 def lex_c_code nested = 0 code = +'' reset_first_position until @scanner.eos? do case when @scanner.scan(/{/) code << @scanner.matched nested += 1 when @scanner.scan(/}/) if nested == 0 && @end_symbol == '}' @scanner.unscan return [:C_DECLARATION, Lrama::Lexer::Token::UserCode.new(s_value: code, location: location)] else code << @scanner.matched nested -= 1 end when @scanner.check(/#{@end_symbol}/) return [:C_DECLARATION, Lrama::Lexer::Token::UserCode.new(s_value: code, location: location)] when @scanner.scan(/\n/) code << @scanner.matched newline when @scanner.scan(/".*?"/) code << %Q(#{@scanner.matched}) @line += @scanner.matched.count("\n") when @scanner.scan(/'.*?'/) code << %Q(#{@scanner.matched}) when @scanner.scan(/[^\"'\{\}\n]+/) code << @scanner.matched when @scanner.scan(/#{Regexp.escape(@end_symbol)}/) # steep:ignore code << @scanner.matched else code << @scanner.getch end end raise ParseError, location.("Unexpected code: #{code}") # steep:ignore UnknownConstant end |
#lex_token ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/lrama/lexer.rb', line 101 def lex_token until @scanner.eos? do case when @scanner.scan(/\n/) newline when @scanner.scan(/\s+/) @scanner.matched.count("\n").times { newline } when @scanner.scan(/\/\*/) lex_comment when @scanner.scan(/\/\/.*(?<newline>\n)?/) newline if @scanner[:newline] else break end end reset_first_position case when @scanner.eos? return when @scanner.scan(/#{SYMBOLS.join('|')}/) return [@scanner.matched, Lrama::Lexer::Token::Token.new(s_value: @scanner.matched, location: location)] when @scanner.scan(/#{PERCENT_TOKENS.join('|')}/) return [@scanner.matched, Lrama::Lexer::Token::Token.new(s_value: @scanner.matched, location: location)] when @scanner.scan(/[\?\+\*]/) return [@scanner.matched, Lrama::Lexer::Token::Token.new(s_value: @scanner.matched, location: location)] when @scanner.scan(/<\w+>/) return [:TAG, Lrama::Lexer::Token::Tag.new(s_value: @scanner.matched, location: location)] when @scanner.scan(/'.'/) return [:CHARACTER, Lrama::Lexer::Token::Char.new(s_value: @scanner.matched, location: location)] when @scanner.scan(/'\\\\'|'\\b'|'\\t'|'\\f'|'\\r'|'\\n'|'\\v'|'\\13'/) return [:CHARACTER, Lrama::Lexer::Token::Char.new(s_value: @scanner.matched, location: location)] when @scanner.scan(/".*?"/) return [:STRING, Lrama::Lexer::Token::Str.new(s_value: %Q(#{@scanner.matched}), location: location)] when @scanner.scan(/\d+/) return [:INTEGER, Lrama::Lexer::Token::Int.new(s_value: Integer(@scanner.matched), location: location)] when @scanner.scan(/([a-zA-Z_.][-a-zA-Z0-9_.]*)/) token = Lrama::Lexer::Token::Ident.new(s_value: @scanner.matched, location: location) type = if @scanner.check(/\s*(\[\s*[a-zA-Z_.][-a-zA-Z0-9_.]*\s*\])?\s*:/) :IDENT_COLON else :IDENTIFIER end return [type, token] else raise ParseError, location.("Unexpected token") # steep:ignore UnknownConstant end end |
#location ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/lrama/lexer.rb', line 92 def location Location.new( grammar_file: @grammar_file, first_line: @head_line, first_column: @head_column, last_line: line, last_column: column ) end |
#next_token ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/lrama/lexer.rb', line 77 def next_token case @status when :initial lex_token when :c_declaration lex_c_code end end |