Class: Gamefic::Syntax
- Inherits:
-
Object
- Object
- Gamefic::Syntax
- Defined in:
- lib/gamefic/syntax.rb,
lib/gamefic/syntax/template.rb
Overview
Syntaxes provide rules for matching input patterns to existing responses. Common uses are to provide synonyms for response verbs and allow for variations in sentence structure.
The template and command patterns use words beginning with a colon (e.g., ‘:thing`) to identify phrases that should be tokenized into arguments.
Defined Under Namespace
Classes: Template
Instance Attribute Summary collapse
-
#command ⇒ String
readonly
The pattern that will be used to tokenize the input into a command.
-
#template ⇒ Template
readonly
The pattern that matching input is expected to follow.
-
#verb ⇒ Symbol
readonly
The response verb to which the command will be translated.
Class Method Summary collapse
- .literal_or_nil(string) ⇒ Symbol?
-
.tokenize(text, syntaxes) ⇒ Array<Expression>
Tokenize an array of commands from the specified text.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#accept?(text) ⇒ Boolean
Determine if the specified text matches the syntax’s expected pattern.
-
#compare(other) ⇒ Object
Compare two syntaxes for the purpose of ordering them in rulebooks.
-
#initialize(template, command) ⇒ Syntax
constructor
A new instance of Syntax.
-
#signature ⇒ Object
Get a signature that identifies the form of the Syntax.
-
#synonym ⇒ Symbol
A symbol for the first word in the template.
-
#tokenize(text) ⇒ Expression?
Convert a String into a Command.
Constructor Details
#initialize(template, command) ⇒ Syntax
Returns a new instance of Syntax.
42 43 44 45 46 47 |
# File 'lib/gamefic/syntax.rb', line 42 def initialize template, command @template = Template.to_template(template) @command = command.normalize @verb = Syntax.literal_or_nil(@command.keywords[0]) @replace = parse_replace end |
Instance Attribute Details
#command ⇒ String (readonly)
The pattern that will be used to tokenize the input into a command.
29 30 31 |
# File 'lib/gamefic/syntax.rb', line 29 def command @command end |
#template ⇒ Template (readonly)
The pattern that matching input is expected to follow.
24 25 26 |
# File 'lib/gamefic/syntax.rb', line 24 def template @template end |
#verb ⇒ Symbol (readonly)
The response verb to which the command will be translated.
38 39 40 |
# File 'lib/gamefic/syntax.rb', line 38 def verb @verb end |
Class Method Details
.literal_or_nil(string) ⇒ Symbol?
113 114 115 |
# File 'lib/gamefic/syntax.rb', line 113 def self.literal_or_nil string string.start_with?(':') ? nil : string.to_sym end |
.tokenize(text, syntaxes) ⇒ Array<Expression>
Tokenize an array of commands from the specified text. The resulting array is in descending order of precision, i.e., most to least matched tokens.
98 99 100 101 102 103 |
# File 'lib/gamefic/syntax.rb', line 98 def self.tokenize text, syntaxes syntaxes .map { |syn| syn.tokenize(text) } .compact .sort { |syn, other_syn| syn.compare other_syn } end |
Instance Method Details
#==(other) ⇒ Object
87 88 89 |
# File 'lib/gamefic/syntax.rb', line 87 def ==(other) signature == other&.signature end |
#accept?(text) ⇒ Boolean
Determine if the specified text matches the syntax’s expected pattern.
76 77 78 |
# File 'lib/gamefic/syntax.rb', line 76 def accept? text !!text.match(template.regexp) end |
#compare(other) ⇒ Object
Compare two syntaxes for the purpose of ordering them in rulebooks.
107 108 109 |
# File 'lib/gamefic/syntax.rb', line 107 def compare other template.compare other.template end |
#signature ⇒ Object
Get a signature that identifies the form of the Syntax. Signatures are used to compare Syntaxes to each other.
83 84 85 |
# File 'lib/gamefic/syntax.rb', line 83 def signature [template.regexp, replace] end |
#synonym ⇒ Symbol
A symbol for the first word in the template. Used by rulebooks to classify groups of related syntaxes.
57 58 59 |
# File 'lib/gamefic/syntax.rb', line 57 def synonym template.verb end |
#tokenize(text) ⇒ Expression?
Convert a String into a Command.
65 66 67 68 69 70 |
# File 'lib/gamefic/syntax.rb', line 65 def tokenize text match = text&.match(template.regexp) return nil unless match Expression.new(verb, match_to_args(match)) end |