Class: Ruty::Parser::ArgumentLexer

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

Overview

class for parsing arguments. used by the parse_arguments function. It’s usualy a better idea to not use this class yourself because it’s pretty low level.

Constant Summary collapse

WHITESPACE_RE =

lexer constants

/\s+/m
NAME_RE =
/[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*/
PIPE_RE =
/\|/
FILTER_END_RE =
/;/
SEPARATOR_RE =
/,/
STRING_RE =
/
  (?:
    "([^"\\]*(?:\\.[^"\\]*)*)"
  |
    '([^'\\]*(?:\\.[^'\\]*)*)'
  )
/xm
NUMBER_RE =
/\d+(\.\d*)?/

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ ArgumentLexer

create a new ArgumentLexer for the given string



131
132
133
# File 'lib/ruty/parser.rb', line 131

def initialize string
  @string = string
end

Instance Method Details

#lex(&block) ⇒ Object

lex the string. For each return token this function calls the given block with the token type and value.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
191
192
193
# File 'lib/ruty/parser.rb', line 137

def lex &block
  s = StringScanner.new(@string)
  state = :initial

  while not s.eos?
    # supress whitespace. no matter in which state we are.
    next if s.scan(WHITESPACE_RE)

    # normal data, no filters.
    if state == :initial
      if match = s.scan(NAME_RE)
        block.call(:name, match)
      elsif s.scan(PIPE_RE)
        state = :filter
        block.call(:filter_start, nil)
      elsif s.scan(SEPARATOR_RE)
        block.call(:separator, nil)
      elsif match = s.scan(STRING_RE)
        block.call(:string, match)
      elsif match = s.scan(NUMBER_RE)
        block.call(:number, match)
      else
        # nothing matched and we are not at the end of
        # the string, raise an error
        raise TemplateSyntaxError, 'unexpected character ' \
                                   "'#{s.getch}' in block tag"
      end
    
    # filter syntax
    elsif state == :filter
      # if a second pipe occours we start a new filter
      if s.scan(PIPE_RE)
        block.call(:filter_end, nil)
        block.call(:filter_start, nil)
      # filter ends with ; -- handle that
      elsif s.scan(FILTER_END_RE)
        block.call(:filter_end, nil)
        state = :initial
      elsif s.scan(SEPARATOR_RE)
        block.call(:separator, nil)
      # variables, strings and numbers
      elsif match = s.scan(NAME_RE)
        block.call(:name, match)
      elsif match = s.scan(STRING_RE)
        block.call(:string, match)
      elsif match = s.scan(NUMBER_RE)
        block.call(:number, match)
      else
        # nothing matched and we are not at the end of
        # the string, raise an error
        raise TemplateSyntaxError, 'unexpected character ' \
                                   "'#{s.getch}' in filter def"
      end
    end
  end
  block.call(:filter_end, nil) if state == :filter
end