Class: SrcLexer::Lexer
- Inherits:
-
Object
- Object
- SrcLexer::Lexer
- Defined in:
- lib/src_lexer.rb
Direct Known Subclasses
Defined Under Namespace
Classes: PosInfo, StringIterator
Constant Summary collapse
- END_TOKEN =
[false, nil]
- NUMBER_REGEX =
/^[\d]+[\.]?[\d]*\z/
- STRING_REGEX =
/^\"(.*)\"\z/m
Instance Attribute Summary collapse
-
#comment_markers ⇒ Object
readonly
Returns the value of attribute comment_markers.
-
#keywords ⇒ Object
readonly
Returns the value of attribute keywords.
-
#line_comment_marker ⇒ Object
readonly
Returns the value of attribute line_comment_marker.
-
#str ⇒ Object
readonly
Returns the value of attribute str.
-
#string_literal_marker ⇒ Object
readonly
Returns the value of attribute string_literal_marker.
-
#symbols ⇒ Object
readonly
Returns the value of attribute symbols.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #analyze(str) ⇒ Object
-
#initialize(keywords, symbols, string_literal_marker, line_comment_marker, comment_markers) ⇒ Lexer
constructor
A new instance of Lexer.
- #pop_token ⇒ Object
Constructor Details
#initialize(keywords, symbols, string_literal_marker, line_comment_marker, comment_markers) ⇒ Lexer
Returns a new instance of Lexer.
25 26 27 28 29 30 31 |
# File 'lib/src_lexer.rb', line 25 def initialize(keywords, symbols, string_literal_marker, line_comment_marker, comment_markers) @keywords = (keywords ? keywords.uniq.compact : []) @symbols = (symbols ? symbols.uniq.compact : []) @string_literal_marker = string_literal_marker @line_comment_marker = line_comment_marker @comment_markers = comment_markers end |
Instance Attribute Details
#comment_markers ⇒ Object (readonly)
Returns the value of attribute comment_markers.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def comment_markers @comment_markers end |
#keywords ⇒ Object (readonly)
Returns the value of attribute keywords.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def keywords @keywords end |
#line_comment_marker ⇒ Object (readonly)
Returns the value of attribute line_comment_marker.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def line_comment_marker @line_comment_marker end |
#str ⇒ Object (readonly)
Returns the value of attribute str.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def str @str end |
#string_literal_marker ⇒ Object (readonly)
Returns the value of attribute string_literal_marker.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def string_literal_marker @string_literal_marker end |
#symbols ⇒ Object (readonly)
Returns the value of attribute symbols.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def symbols @symbols end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
23 24 25 |
# File 'lib/src_lexer.rb', line 23 def tokens @tokens end |
Instance Method Details
#analyze(str) ⇒ Object
33 34 35 36 |
# File 'lib/src_lexer.rb', line 33 def analyze(str) @str = str tokenize end |
#pop_token ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/src_lexer.rb', line 38 def pop_token token = @tokens.shift return END_TOKEN if token.nil? case token[0] when NUMBER_REGEX [:NUMBER, Token.new(token[0], token[1], token[2])] when STRING_REGEX [:STRING, Token.new(token[0], token[1], token[2])] else [is_reserved?(token[0]) ? token[0] : :IDENT, Token.new(token[0], token[1], token[2])] end end |