42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/magister_cli/parser.rb', line 42
def parse()
tokens = @tokenizer.tokenize
i = 0
ast = Array.new
while i < tokens.length
tmp = 0
props = false
opts = false
altOpts = false
if tokens[0 + i]["type"] != "KEYWORD"
puts "Current index: #{i.to_s}"
raise SyntaxError.new "Expected #{tokens[0 + i]["value"]} to be a KEYWORD, is a #{tokens[0 + i]["type"]}"
end
if tokens[1 + i] != nil && tokens[1 + i]["type"] == "PARAMS"
tmp += 1
props = true
end
if tokens[2 + i] != nil && tokens[2 + i]["type"] == "OPTIONS"
tmp += 1
opts = true
end
if tokens[1 + i] != nil && tokens[1 + i]["type"] == "OPTIONS"
if opts || props
raise SyntaxError.new "Cant have type OPTIONS here, must be PARAMS"
end
opts = true
altOpts = true
tmp += 1
end
ast_node = {"action" => tokens[0 + i]["value"]}
if props
ast_node["parameters"] = tokens[1 + i]["value"]
end
if opts
if altOpts
ast_node["options"] = tokens[1 + i]["value"]
else
ast_node["options"] = tokens[2 + i]["value"]
end
end
if !is_action? ast_node["action"]
throw SyntaxError.new "#{ast_node["action"]} is not a valid action!"
end
lacking = Array.new
lacking = get_args(ast_node["action"])
if props
ast_node["parameters"].each do |prop|
if !lacking.include? prop["key"]
raise SyntaxError.new "#{prop["key"]} is is already defined or is not a valid parameter for #{ast_node["action"]}!"
end
lacking.delete prop["key"]
end
else
ast_node["parameters"] = Array.new
end
ast_node["lacking_params"] = lacking
ast.append(ast_node)
i += tmp + 1 end
ast
end
|