Class: Dhaka::LexerSpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/dhaka/lexer/specification.rb

Class Method Summary collapse

Class Method Details

.for_pattern(pattern, &blk) ⇒ Object

Associates blk as the action to be performed when a lexer recognizes pattern. When Lexer#lex is invoked, it creates a LexerRun object that provides the context for blk to be evaluated in. Methods available in this block are LexerRun#current_lexeme and LexerRun#create_token.



51
52
53
54
55
56
57
58
# File 'lib/dhaka/lexer/specification.rb', line 51

def for_pattern(pattern, &blk)
  source = case pattern
             when String then pattern
             when Regexp then pattern.source
           end
  items[source] = LexerSpecificationItem.new(source, priority, blk)
  self.priority += 1
end

.for_symbol(symbol, &blk) ⇒ Object

Use this to automatically handle escaping for regular expression metacharacters. For example,

for_symbol('+') { ... }

translates to:

for_pattern('\+') { ... }


64
65
66
67
68
69
70
# File 'lib/dhaka/lexer/specification.rb', line 64

def for_symbol(symbol, &blk)
  if LexerSupport::OPERATOR_CHARACTERS.include?(symbol)
    for_pattern("\\#{symbol}", &blk)
  else
    for_pattern(symbol, &blk)
  end
end