Class: RKelly::Parser

Inherits:
GeneratedParser show all
Defined in:
lib/rkelly/parser.rb

Constant Summary collapse

TOKENIZER =
Tokenizer.new

Constants inherited from GeneratedParser

GeneratedParser::Racc_arg, GeneratedParser::Racc_debug_parser, GeneratedParser::Racc_token_to_s_table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GeneratedParser

#_reduce_none

Constructor Details

#initializeParser

Returns a new instance of Parser.



30
31
32
33
34
35
36
# File 'lib/rkelly/parser.rb', line 30

def initialize
  @tokens = []
  @logger = nil
  @terminator = false
  @prev_token = nil
  @comments = []
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



29
30
31
# File 'lib/rkelly/parser.rb', line 29

def logger
  @logger
end

Instance Method Details

#parse(javascript, filename = nil) ⇒ Object

Parse javascript and return an AST



39
40
41
42
43
44
45
46
# File 'lib/rkelly/parser.rb', line 39

def parse(javascript, filename = nil)
  @tokens = TOKENIZER.raw_tokens(javascript)
  @position = 0
  @filename = filename
  ast = do_parse
  ast.comments = @comments if ast
  ast
end

#stopped_atObject

When parsing finishes without all tokens being parsed, returns the token at which the parsing stopped. Returns nil when parser reached to the very last token (but possibly still failed as it expeced more tokens).

Useful for pin-pointing the position of a syntax error.



58
59
60
61
62
63
64
# File 'lib/rkelly/parser.rb', line 58

def stopped_at
  if @position < @tokens.length
    @tokens[@position-1]
  else
    nil
  end
end

#yyabortObject



48
49
50
# File 'lib/rkelly/parser.rb', line 48

def yyabort
  raise "something bad happened, please report a bug with sample JavaScript"
end