Class: Gamefic::Syntax

Inherits:
Object
  • Object
show all
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.

Examples:

All of these syntaxes will translate input into a command of the

form "look thing container"

  Syntax.new('examine :thing in :container', 'look :thing :container')
  Syntax.new('look at :thing inside :container', 'look :thing :container')
  Syntax.new('search :container for :thing', 'look :thing :container')

Defined Under Namespace

Classes: Template

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, command) ⇒ Syntax

Returns a new instance of Syntax.

Parameters:



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

#commandString (readonly)

The pattern that will be used to tokenize the input into a command.

Returns:



29
30
31
# File 'lib/gamefic/syntax.rb', line 29

def command
  @command
end

#templateTemplate (readonly)

The pattern that matching input is expected to follow.

Returns:



24
25
26
# File 'lib/gamefic/syntax.rb', line 24

def template
  @template
end

#verbSymbol (readonly)

The response verb to which the command will be translated.

Examples:

syntax = Syntax.new('examine :thing', 'look :thing')
syntax.verb #=> :look

Returns:

  • (Symbol)


38
39
40
# File 'lib/gamefic/syntax.rb', line 38

def verb
  @verb
end

Class Method Details

.literal_or_nil(string) ⇒ Symbol?

Parameters:

Returns:

  • (Symbol, nil)


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.

Parameters:

  • text (String)

    The text to tokenize.

  • syntaxes (Array<Syntax>)

    The syntaxes to use.

Returns:



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.

Parameters:

Returns:

  • (Boolean)


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

#signatureObject

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

#synonymSymbol

A symbol for the first word in the template. Used by rulebooks to classify groups of related syntaxes.

Examples:

syntax = Syntax.new('examine :thing', 'look :thing')
syntax.synonym #=> :examine

Returns:

  • (Symbol)


57
58
59
# File 'lib/gamefic/syntax.rb', line 57

def synonym
  template.verb
end

#tokenize(text) ⇒ Expression?

Convert a String into a Command.

Parameters:

Returns:



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