Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/magister_cli/parser.rb

Constant Summary collapse

@@actions =
[
    ['login', ['username', 'password', 'school']],
    ['auth', ['token', 'school']],
    ['quit', []],
    ['exit', []],
    ['get_classes', ['date_from', 'date_to']]
]

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.



15
16
17
18
# File 'lib/magister_cli/parser.rb', line 15

def initialize(string)
    @tokenizer = Tokenizer.new(string)
    @string = string
end

Instance Method Details

#get_args(actionName) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/magister_cli/parser.rb', line 34

def get_args(actionName)
    @@actions.each do |action|
        if action[0] == actionName
            return action[1].dup
        end
    end
end

#is_action?(actionName) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/magister_cli/parser.rb', line 20

def is_action?(actionName)
    @@actions.each do |action|
        if action[0] == actionName
            return true
        end
    end
    return false
end

#parseObject



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 # true if opts are in the place props would be

        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 # + 1 for the keyword itself
    end

    ast
end

#string=(string) ⇒ Object



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

def string=(string)
    @string = string
    @tokenizer = Tokenizer.new(string)
end