Class: SrcLexer::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/src_lexer.rb

Direct Known Subclasses

CSharpLexer

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

Instance Method Summary collapse

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_markersObject (readonly)

Returns the value of attribute comment_markers.



23
24
25
# File 'lib/src_lexer.rb', line 23

def comment_markers
  @comment_markers
end

#keywordsObject (readonly)

Returns the value of attribute keywords.



23
24
25
# File 'lib/src_lexer.rb', line 23

def keywords
  @keywords
end

#line_comment_markerObject (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

#strObject (readonly)

Returns the value of attribute str.



23
24
25
# File 'lib/src_lexer.rb', line 23

def str
  @str
end

#string_literal_markerObject (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

#symbolsObject (readonly)

Returns the value of attribute symbols.



23
24
25
# File 'lib/src_lexer.rb', line 23

def symbols
  @symbols
end

#tokensObject (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_tokenObject



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