Class: Tokenizer
- Inherits:
-
Object
- Object
- Tokenizer
- Defined in:
- lib/magister_cli/tokenizer.rb
Overview
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at mozilla.org/MPL/2.0/.
Constant Summary collapse
- @@spec =
[ [/(\w+)/, "KEYWORD"], [/(?:\w+\s?){(.*)}/, "PARAMS"], [/(?:\w+\s?(?:{.*}\s?)?)\[(.*)\]/, "OPTIONS"], [/(\s+)/, "EMPTY"] ]
Instance Method Summary collapse
-
#initialize(string) ⇒ Tokenizer
constructor
A new instance of Tokenizer.
- #parse_options(optionsString) ⇒ Object
- #tokenize ⇒ Object
Constructor Details
#initialize(string) ⇒ Tokenizer
Returns a new instance of Tokenizer.
13 14 15 |
# File 'lib/magister_cli/tokenizer.rb', line 13 def initialize(string) @string = string end |
Instance Method Details
#parse_options(optionsString) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 |
# File 'lib/magister_cli/tokenizer.rb', line 17 def () cursor = 0 found = Array.new while cursor < .length scope = [cursor..-1] if scope[0] == "{" || scope[0] == "}" || scope[0] == "[" || scope[0] == "]" cursor += 1 next end whiteSpaceMatch = scope.match /^\s+/ if whiteSpaceMatch != nil cursor += whiteSpaceMatch[0].length next end keywordMatch = scope.match /^\w+\s?:/ if keywordMatch != nil cursor += keywordMatch[0].length found.append({"type" => "KEYWORD", "value" => keywordMatch[0][0..-2]}) next end explicitString = scope.match /^'([^']*)',*/ if explicitString != nil cursor += explicitString[0].length found.append({"type" => "STRING", "value" => explicitString.captures[0]}) next end impliedString = scope.match /^(.*?),*/ if impliedString != nil cursor += impliedString[0].length found.append({"type" => "STRING", "value" => impliedString.captures[0]}) next end end parsed = Array.new i = 0 while i < found.length if found[i]["type"] != "KEYWORD" || found[i + 1]["type"] != "STRING" raise TypeError.new "Expected #{found[i]["value"]} to be a KEYWORD and #{found[i + 1]["value"]} to be a STRING" end parsed.append({"type" => "PROPERTY", "key" => found[i]["value"], "value" => found[i + 1]["value"]}) i += 2 end parsed end |
#tokenize ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/magister_cli/tokenizer.rb', line 69 def tokenize @tokens = Array.new @@spec.each do |item| matched = @string.match item[0] if matched != nil matched.captures.each do |match| if item[1] == "PARAMS" || item[1] == "OPTIONS" @tokens.append({"type" => item[1], "value" => (match)}) elsif item[1] == "EMPTY" else @tokens.append({"type" => item[1], "value" => match}) end end end end @tokens end |