Class: MixParser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/ruby/parser.rb,
lib/ruby/mix.tab.rb

Overview

Copyright © 2011-2012 Jesse Sielaff

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"\"!\"",
"\"-\u{b7}\"",
"NEGATIVE",
"\"*\"",
"\"/\"",
"\"%\"",
"\"+\"",
"\"-\"",
"\"<<\"",
"\">>\"",
"\"<\"",
"\"<=\"",
"\">\"",
"\">=\"",
"\"==\"",
"\"!=\"",
"\"&&\"",
"\"||\"",
"\"?\"",
"\":\"",
"\"=\"",
"\"&&=\"",
"\"||=\"",
"\"\u{b7}=\"",
"IF",
"UNLESS",
"WHILE",
"UNTIL",
"\";\"",
"NEWLINE",
"MIXIN",
"INDENT",
"OUTDENT",
"IDENTIFIER",
"\"#\"",
"\"|\"",
"\",\"",
"SWITCH",
"ELSIF",
"ELSE",
"CASE",
"BREAK",
"RETURN",
"\"(\"",
"\")\"",
"\".\"",
"\"\u{b7}[\"",
"\"]\"",
"\"\u{b7}#\"",
"\"\u{b7}:\"",
"NUMBER",
"STRING",
"APP",
"FALSE",
"NULL",
"SELF",
"TRUE",
"\"[\"",
"\"{\"",
"\"}\"",
"KEY",
"$start",
"statements",
"statement",
"indented_statement",
"inline_statements",
"opt_semicolon",
"newline",
"mixin_statement",
"control_statement",
"function_statement",
"mixin_body",
"definitions",
"definition",
"function_block",
"inline_function",
"parameter_list",
"indent_block",
"parameters",
"@1",
"exp",
"else_statement",
"opt_else",
"cases",
"case",
"inline_statement",
"postfix_statement",
"mix_statement",
"keyword_statement",
"object",
"dot_access",
"arguments",
"functional_object",
"lhs",
"literal",
"literal_number",
"literal_string",
"literal_constant",
"literal_array",
"literal_object",
"opt_comma",
"fields",
"parenthetical",
"call",
"curly_brace_block" ]
Racc_debug_parser =
false

Instance Method Summary collapse

Instance Method Details

#_reduce_none(val, _values) ⇒ Object



1381
1382
1383
# File 'lib/ruby/mix.tab.rb', line 1381

def _reduce_none(val, _values)
  val[0]
end

#append_statement(outermost_statement, final_statement) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/ruby/parser.rb', line 7

def append_statement (outermost_statement, final_statement)
  current = outermost_statement
  
  current = current[:values][1] while current[:values][1]
  current[:values][1] = final_statement
  
  outermost_statement
end

#newline(statement_node) ⇒ Object



16
17
18
19
# File 'lib/ruby/parser.rb', line 16

def newline (statement_node)
  statement_node[:values][0] = node(:newline, *@location, statement_node[:values][0])
  statement_node
end

#next_tokenObject



21
22
23
# File 'lib/ruby/parser.rb', line 21

def next_token
  @token_stream.shift || [false, false]
end

#node(type, *values) ⇒ Object



25
26
27
# File 'lib/ruby/parser.rb', line 25

def node (type, *values)
  { type: type, values: values }
end

#on_error(token, value, stack) ⇒ Object

Raises:

  • (SyntaxError)


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

def on_error (token, value, stack)
  if value
    value, file, line = value[0].inspect, *value[1]
  else
    value, file, line = 'EOF', *@location
  end
  
  raise SyntaxError, "Unexpected token #{token_to_str(token)} (#{value}): #{file}:#{line}".gsub(?",'\"')
end

#parameter(name) ⇒ Object



39
40
41
42
# File 'lib/ruby/parser.rb', line 39

def parameter (name)
  @variables[:parameters] << name
  @variables[:local] << name
end

#parse(tokens) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ruby/parser.rb', line 44

def parse (tokens)
  @location = ['',0]
  @token_stream = tokens
  @variables = { parameters: [], local: [], embedded: [], prev: nil }
  
  return do_parse
end

#pop_variablesObject



52
53
54
55
56
57
# File 'lib/ruby/parser.rb', line 52

def pop_variables
  vars = [@variables[:parameters], @variables[:embedded]]
  @variables = @variables[:prev]
  
  return vars
end

#push_variablesObject



59
60
61
62
# File 'lib/ruby/parser.rb', line 59

def push_variables
  embedded = @variables[:local] + @variables[:embedded]
  @variables = { parameters: [], local: [], embedded: embedded, prev: @variables }
end

#variable(name) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/ruby/parser.rb', line 64

def variable (name)
  unless (@variables[:local] + @variables[:embedded]).include?(name)
    @variables[:local] << name
  end
  
  node :variable, name
end