Class: Gamefic::Syntax
- Inherits:
-
Object
- Object
- Gamefic::Syntax
- Defined in:
- lib/gamefic/syntax.rb
Constant Summary collapse
- @@phrase =
'([\w\W\s\S]*?)'
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#first_word ⇒ Object
readonly
Returns the value of attribute first_word.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#token_count ⇒ Object
readonly
Returns the value of attribute token_count.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Class Method Summary collapse
-
.tokenize(text, syntaxes) ⇒ Array<Command>
Tokenize an Array of Commands from the specified text.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(template, *command) ⇒ Syntax
constructor
A new instance of Syntax.
-
#signature ⇒ Object
Get a signature that identifies the form of the Syntax.
-
#tokenize(text) ⇒ Command
Convert a String into a Command.
Constructor Details
#initialize(template, *command) ⇒ Syntax
Returns a new instance of Syntax.
9 10 11 12 13 14 15 16 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 |
# File 'lib/gamefic/syntax.rb', line 9 def initialize template, *command command = command.join(' ') words = template.split_words @token_count = words.length command_words = command.split_words @verb = nil if words[0][0] == ':' @token_count -= 1 @first_word = '' else @verb = command_words[0].to_sym if !command_words[0].nil? @first_word = words[0].to_s end @command = command_words.join(' ') @template = words.join(' ') tokens = [] variable_tokens = [] last_token_is_reg = false words.each { |w| if w.match(/^:[a-z0-9_]+$/i) variable_tokens.push w if last_token_is_reg next else tokens.push @@phrase last_token_is_reg = true end else tokens.push w last_token_is_reg = false end } subs = [] index = 0 command_words.each { |t| if t[0] == ':' index = variable_tokens.index(t) + 1 subs.push "{$#{index}}" else subs.push t end } @replace = subs.join(' ') @regexp = Regexp.new("^#{tokens.join(' ')}$", Regexp::IGNORECASE) end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/gamefic/syntax.rb', line 6 def command @command end |
#first_word ⇒ Object (readonly)
Returns the value of attribute first_word.
6 7 8 |
# File 'lib/gamefic/syntax.rb', line 6 def first_word @first_word end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
6 7 8 |
# File 'lib/gamefic/syntax.rb', line 6 def template @template end |
#token_count ⇒ Object (readonly)
Returns the value of attribute token_count.
6 7 8 |
# File 'lib/gamefic/syntax.rb', line 6 def token_count @token_count end |
#verb ⇒ Object (readonly)
Returns the value of attribute verb.
6 7 8 |
# File 'lib/gamefic/syntax.rb', line 6 def verb @verb end |
Class Method Details
.tokenize(text, syntaxes) ⇒ Array<Command>
Tokenize an Array of Commands from the specified text.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/gamefic/syntax.rb', line 89 def self.tokenize text, syntaxes matches = [] syntaxes.each { |syntax| result = syntax.tokenize text matches.push(result) if !result.nil? } # Sort matches having the most populated arguments first matches.sort! { |a, b| ca = 0 cb = 0 a.arguments.each { |t| break if t.nil? ca += 1 } b.arguments.each { |t| break if t.nil? cb += 1 } cb <=> ca } matches end |
Instance Method Details
#==(other) ⇒ Object
80 81 82 |
# File 'lib/gamefic/syntax.rb', line 80 def ==(other) signature == other.signature end |
#signature ⇒ Object
Get a signature that identifies the form of the Syntax. Signatures are used to compare Syntaxes to each other.
76 77 78 |
# File 'lib/gamefic/syntax.rb', line 76 def signature [@regexp, @replace] end |
#tokenize(text) ⇒ Command
Convert a String into a Command.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gamefic/syntax.rb', line 59 def tokenize text m = text.match(@regexp) return nil if m.nil? arguments = [] @replace.to_s.split_words.each { |r| if r.match(/^\{\$[0-9]+\}$/) arguments.push m[r[2..-2].to_i] else arguments.push r end } Command.new @verb, arguments end |