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.



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

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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#parse(javascript, filename = nil) ⇒ Object

Parse javascript and return an AST



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

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.



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

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

#yyabortObject



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

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