Class: Lrama::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/lrama/lexer.rb,
lib/lrama/lexer/token.rb,
lib/lrama/lexer/token/type.rb

Defined Under Namespace

Classes: Token

Constant Summary collapse

SYMBOLS =
%w(%{ %} %% { } \[ \] : \| ;)
PERCENT_TOKENS =
%w(
  %union
  %token
  %type
  %left
  %right
  %nonassoc
  %expect
  %define
  %require
  %printer
  %lex-param
  %parse-param
  %initial-action
  %precedence
  %prec
  %error-token
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Lexer

Returns a new instance of Lexer.



29
30
31
32
33
34
35
# File 'lib/lrama/lexer.rb', line 29

def initialize(text)
  @scanner = StringScanner.new(text)
  @head = @scanner.pos
  @line = 1
  @status = :initial
  @end_symbol = nil
end

Instance Attribute Details

#end_symbolObject

Returns the value of attribute end_symbol.



7
8
9
# File 'lib/lrama/lexer.rb', line 7

def end_symbol
  @end_symbol
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/lrama/lexer.rb', line 6

def status
  @status
end

Instance Method Details

#columnObject



50
51
52
# File 'lib/lrama/lexer.rb', line 50

def column
  @scanner.pos - @head
end

#lex_c_codeObject



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
# File 'lib/lrama/lexer.rb', line 107

def lex_c_code
  nested = 0
  code = ''
  while !@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, build_token(type: Token::User_code, s_value: code, references: [])]
      else
        code += @scanner.matched
        nested -= 1
      end
    when @scanner.check(/#{@end_symbol}/)
      return [:C_DECLARATION, build_token(type: Token::User_code, s_value: code, references: [])]
    when @scanner.scan(/\n/)
      code += @scanner.matched
      newline
    when @scanner.scan(/"/)
      matched = @scanner.scan_until(/"/)
      code += %Q("#{matched})
      @line += matched.count("\n")
    when @scanner.scan(/'/)
      matched = @scanner.scan_until(/'/)
      code += %Q('#{matched})
    else
      code += @scanner.getch
    end
  end
  raise
end

#lex_tokenObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lrama/lexer.rb', line 54

def lex_token
  while !@scanner.eos? do
    case
    when @scanner.scan(/\n/)
      newline
    when @scanner.scan(/\s+/)
      # noop
    when @scanner.scan(/\/\*/)
      lex_comment
    when @scanner.scan(/\/\//)
      @scanner.scan_until(/\n/)
      newline
    when @scanner.scan(/%empty/)
      # noop
    else
      break
    end
  end

  @head_line = line
  @head_column = column

  case
  when @scanner.eos?
    return
  when @scanner.scan(/#{SYMBOLS.join('|')}/)
    return [@scanner.matched, @scanner.matched]
  when @scanner.scan(/#{PERCENT_TOKENS.join('|')}/)
    return [@scanner.matched, @scanner.matched]
  when @scanner.scan(/<\w+>/)
    return [:TAG, build_token(type: Token::Tag, s_value: @scanner.matched)]
  when @scanner.scan(/'.'/)
    return [:CHARACTER, build_token(type: Token::Char, s_value: @scanner.matched)]
  when @scanner.scan(/'\\\\'|'\\b'|'\\t'|'\\f'|'\\r'|'\\n'|'\\v'|'\\13'/)
    return [:CHARACTER, build_token(type: Token::Char, s_value: @scanner.matched)]
  when @scanner.scan(/"/)
    return [:STRING, %Q("#{@scanner.scan_until(/"/)})]
  when @scanner.scan(/\d+/)
    return [:INTEGER, Integer(@scanner.matched)]
  when @scanner.scan(/([a-zA-Z_.][-a-zA-Z0-9_.]*)/)
    token = build_token(type: Token::Ident, s_value: @scanner.matched)
    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
  end
end

#lineObject



46
47
48
# File 'lib/lrama/lexer.rb', line 46

def line
  @line
end

#next_tokenObject



37
38
39
40
41
42
43
44
# File 'lib/lrama/lexer.rb', line 37

def next_token
  case @status
  when :initial
    lex_token
  when :c_declaration
    lex_c_code
  end
end