Class: RiveScript::Parser

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

Overview

Parser for RiveScript syntax.

Constant Summary collapse

CONCAT_MODES =
{
  "none" => "",
  "newline" => "\n",
  "space" => " "
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(master) ⇒ Parser

Returns a new instance of Parser.



17
18
19
20
21
# File 'lib/rivescript/parser.rb', line 17

def initialize(master)
  @master = master
  @strict = master._strict
  @utf8 = master._utf8
end

Instance Method Details

#check_syntax(cmd, line) ⇒ Object

Check the syntax of a RiveScript command.



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/rivescript/parser.rb', line 493

def check_syntax(cmd, line)
  case cmd
  when "!"
    unless line.match?(/\A.+(?:\s+.+|)\s*=\s*.+?\z/)
      return "Invalid format for !Definition line: must be '! type name = value' OR '! type = value'"
    end

    if line.match?(/^array/)
      if line.match?(/=\s?\||\|\s?$/)
        return "Piped arrays can't begin or end with a |"
      elsif line.match?(/\|\|/)
        return "Piped arrays can't include blank entries"
      end
    end
  when ">"
    parts = line.split(/\s+/)
    if parts[0] == "begin" && parts.length > 1
      return "The 'begin' label takes no additional arguments"
    elsif parts[0] == "topic"
      if !@master._forceCase && line.match?(/[^a-z0-9_\-\s]/)
        return "Topics should be lowercased and contain only letters and numbers"
      elsif line.match?(/[^A-Za-z0-9_\-\s]/)
        return "Topics should contain only letters and numbers in forceCase mode"
      end
    elsif parts[0] == "object"
      if line.match?(/[^A-Za-z0-9_\-\s]/)
        return "Objects can only contain numbers and letters"
      end
    end
  when "+", "%", "@"
    parens = 0
    square = 0
    curly = 0
    angle = 0

    if @utf8
      if line.match?(/[A-Z\\.]/)
        return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode"
      end
    elsif line.match?(/[^a-z0-9(|)\[\]*_#@{}<>=\/\s]/)
      return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # { } < > = /"
    elsif line.match?(/\(\||\|\)/)
      return "Piped alternations can't begin or end with a |"
    elsif line.match?(/\([^\)].+\|\|.+\)/)
      return "Piped alternations can't include blank entries"
    elsif line.match?(/\[\||\|\]/)
      return "Piped optionals can't begin or end with a |"
    elsif line.match?(/\[[^\]].+\|\|.+\]/)
      return "Piped optionals can't include blank entries"
    end

    line.each_char do |char|
      case char
      when "(" then parens += 1
      when ")" then parens -= 1
      when "[" then square += 1
      when "]" then square -= 1
      when "{" then curly += 1
      when "}" then curly -= 1
      when "<" then angle += 1
      when ">" then angle -= 1
      end
    end

    return "Unmatched parenthesis brackets" if parens != 0
    return "Unmatched square brackets" if square != 0
    return "Unmatched curly brackets" if curly != 0
    return "Unmatched angle brackets" if angle != 0
  when "*"
    unless line.match?(/\A.+?\s*(?:==|eq|!=|ne|<>|<|<=|>|>=)\s*.+?=>.+?\z/)
      return "Invalid format for !Condition: should be like '* value symbol value => response'"
    end
  end

  ""
end

#init_topic(topics, name) ⇒ Object

Initialize the topic tree for the parsing phase.



571
572
573
574
575
576
577
578
579
# File 'lib/rivescript/parser.rb', line 571

def init_topic(topics, name)
  return unless topics[name].nil?

  topics[name] = {
    "includes" => {},
    "inherits" => {},
    "triggers" => []
  }
end

#parse(filename, code, on_error = nil) ⇒ Object

Read and parse a RiveScript document.



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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/rivescript/parser.rb', line 33

def parse(filename, code, on_error = nil)
  on_error ||= lambda { |err, fname, lineno| warn(err, fname, lineno) }

  ast = {
    "begin" => {
      "global" => {},
      "var" => {},
      "sub" => {},
      "person" => {},
      "array" => {}
    },
    "topics" => {},
    "objects" => []
  }

  topic = "random"
  comment = false
  inobj = false
  obj_name = ""
  obj_lang = ""
  obj_buf = []
  cur_trig = nil
  is_that = nil

  local_options = {
    "concat" => @master._concat.nil? ? "none" : @master._concat
  }

  lines = code.split("\n")
  lines.each_with_index do |raw_line, lp|
    line = Utils.strip(raw_line)
    lineno = lp + 1

    next if line.empty?

    if inobj
      if line.include?("< object") || line.include?("<object")
        if !obj_name.empty?
          ast["objects"] << {
            "name" => obj_name,
            "language" => obj_lang,
            "code" => obj_buf
          }
        end
        obj_name = ""
        obj_lang = ""
        obj_buf = []
        inobj = false
      else
        obj_buf << line
      end
      next
    end

    if line.start_with?("//")
      next
    elsif line.start_with?("#")
      warn("Using the # symbol for comments is deprecated", filename, lineno)
      next
    elsif line.start_with?("/*")
      if line.include?("*/")
        next
      end

      comment = true
      next
    elsif line.include?("*/")
      comment = false
      next
    end
    next if comment

    if line.length < 2
      warn("Weird single-character line '#{line}' found (in topic #{topic})", filename, lineno)
      next
    end

    cmd = line[0]
    line = Utils.strip(line[1..])

    if line.include?(" //")
      line = Utils.strip(line.split(" //", 2)[0])
    end

    if cmd == "?"
      variants = [
        line,
        "[*]#{line}[*]",
        "*#{line}*",
        "[*]#{line}*",
        "*#{line}[*]",
        "#{line}*",
        "*#{line}"
      ]
      cmd = "+"
      line = "(#{variants.join('|')})"
      say("Rewrote ?Keyword as +Trigger: #{line}")
    end

    if @master._forceCase == true && cmd == "+"
      line = line.downcase
    end

    syntax_error = check_syntax(cmd, line)
    unless syntax_error.empty?
      if @strict
        on_error.call("Syntax error: #{syntax_error} at #{filename} line #{lineno} near #{cmd} #{line}", filename, lineno)
      else
        warn("Syntax error: #{syntax_error} at #{filename} line #{lineno} near #{cmd} #{line} (in topic #{topic})", filename, lineno)
      end
    end

    is_that = nil if cmd == "+"

    say("Cmd: #{cmd}; line: #{line}")

    ((lp + 1)...lines.length).each do |li|
      lookahead = Utils.strip(lines[li])
      next if lookahead.length < 2

      look_cmd = lookahead[0]
      lookahead = Utils.strip(lookahead[1..])

      break unless ["%", "^"].include?(look_cmd)
      break if lookahead.empty?

      say("\tLookahead #{li}: #{look_cmd} #{lookahead}")

      if cmd == "+"
        if look_cmd == "%"
          is_that = lookahead
          break
        else
          is_that = nil
        end
      end

      if cmd == "!"
        if look_cmd == "^"
          line += "<crlf>#{lookahead}"
        end
        next
      end

      if cmd != "^" && look_cmd != "%"
        if look_cmd == "^"
          if CONCAT_MODES.key?(local_options["concat"])
            line += CONCAT_MODES[local_options["concat"]] + lookahead
          else
            line += lookahead
          end
        else
          break
        end
      end
    end

    type = ""
    name = ""

    case cmd
    when "!"
      halves = line.split("=", 2)
      left = Utils.strip(halves[0]).split(" ")
      value = ""
      name = ""
      type = ""
      value = Utils.strip(halves[1]) if halves.length == 2

      if left.length >= 1
        type = Utils.strip(left[0])
        if left.length >= 2
          left.shift
          name = Utils.strip(left.join(" "))
        end
      end

      value = value.gsub("<crlf>", "") unless type == "array"

      if type == "version"
        if value.to_f > RS_VERSION.to_f
          on_error.call("Unsupported RiveScript version. We only support #{RS_VERSION} at #{filename} line #{lineno}", filename, lineno)
          return ast
        end
        next
      end

      if name.empty?
        warn("Undefined variable name", filename, lineno)
        next
      end
      if value.empty?
        warn("Undefined variable value", filename, lineno)
        next
      end

      case type
      when "local"
        say("\tSet local parser option #{name} = #{value}")
        local_options[name] = value
      when "global"
        say("\tSet global #{name} = #{value}")
        ast["begin"]["global"][name] = value
      when "var"
        say("\tSet bot variable #{name} = #{value}")
        ast["begin"]["var"][name] = value
      when "array"
        if value == "<undef>"
          ast["begin"]["array"][name] = "<undef>"
          next
        end

        parts = value.split("<crlf>")
        fields = []
        parts.each do |val|
          if val.include?("|")
            fields.concat(val.split("|"))
          else
            fields.concat(val.split(" "))
          end
        end

        fields.map! { |field| field.gsub(/\\s/i, " ") }
        fields.reject!(&:empty?)

        say("\tSet array #{name} = #{fields.inspect}")
        ast["begin"]["array"][name] = fields
      when "sub"
        say("\tSet substitution #{name} = #{value}")
        ast["begin"]["sub"][name] = value
      when "person"
        say("\tSet person substitution #{name} = #{value}")
        ast["begin"]["person"][name] = value
      else
        warn("Unknown definition type #{type}", filename, lineno)
      end
    when ">"
      temp = Utils.strip(line).split(" ")
      type = temp.shift
      name = ""
      fields = []
      name = temp.shift if temp.length > 0
      fields = temp if temp.length > 0

      case type
      when "begin", "topic"
        if type == "begin"
          say("Found the BEGIN block.")
          type = "topic"
          name = "__begin__"
        end

        name = name.downcase if @master._forceCase == true

        say("Set topic to #{name}")
        cur_trig = nil
        topic = name

        init_topic(ast["topics"], topic)

        mode = ""
        if fields.length >= 2
          fields.each do |field|
            if ["includes", "inherits"].include?(field)
              mode = field
            elsif !mode.empty?
              ast["topics"][topic][mode][field] = 1
            end
          end
        end
      when "object"
        lang = ""
        lang = fields[0].downcase if fields.length > 0

        if lang.empty?
          warn("Trying to parse unknown programming language", filename, lineno)
          lang = "ruby"
        end

        obj_name = name
        obj_lang = lang
        obj_buf = []
        inobj = true
      else
        warn("Unknown label type #{type}", filename, lineno)
      end
    when "<"
      type = line
      if ["begin", "topic"].include?(type)
        say("\tEnd the topic label.")
        topic = "random"
      elsif type == "object"
        say("\tEnd the object label.")
        inobj = false
      end
    when "+"
      say("\tTrigger pattern: #{line}")

      init_topic(ast["topics"], topic)
      cur_trig = {
        "trigger" => line,
        "reply" => [],
        "condition" => [],
        "redirect" => nil,
        "previous" => is_that
      }
      ast["topics"][topic]["triggers"] << cur_trig
    when "-"
      if cur_trig.nil?
        warn("Response found before trigger", filename, lineno)
        next
      end

      if !cur_trig["redirect"].nil?
        warn("You can't mix @Redirects with -Replies", filename, lineno)
      end

      say("\tResponse: #{line}")
      cur_trig["reply"] << line
    when "*"
      if cur_trig.nil?
        warn("Condition found before trigger", filename, lineno)
        next
      end

      if !cur_trig["redirect"].nil?
        warn("You can't mix @Redirects with *Conditions", filename, lineno)
      end

      say("\tCondition: #{line}")
      cur_trig["condition"] << line
    when "%", "^"
      next
    when "@"
      if cur_trig["reply"].length > 0 || cur_trig["condition"].length > 0
        warn("You can't mix @Redirects with -Replies or *Conditions", filename, lineno)
      end
      say("\tRedirect response to: #{line}")
      cur_trig["redirect"] = Utils.strip(line)
    else
      warn("Unknown command '#{cmd}' (in topic #{topic})", filename, lineno)
    end
  end

  ast
end

#say(message) ⇒ Object

Proxy functions



24
25
26
# File 'lib/rivescript/parser.rb', line 24

def say(message)
  @master.say(message)
end

#stringify(deparsed = nil) ⇒ Object

Translate deparsed data into the source code of a RiveScript document.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/rivescript/parser.rb', line 381

def stringify(deparsed = nil)
  deparsed = @master.deparse if deparsed.nil?

  write_triggers = lambda do |triggers, indent|
    id = indent ? "\t" : ""
    output = []
    triggers.each do |t|
      output << "#{id}+ #{t['trigger']}"
      output << "#{id}% #{t['previous']}" if t["previous"]
      t["condition"]&.each do |c|
        output << "#{id}* #{c.gsub("\n", "\\n")}"
      end
      output << "#{id}@ #{t['redirect']}" if t["redirect"]
      t["reply"]&.each do |r|
        output << "#{id}- #{r.gsub("\n", "\\n")}" if r
      end
      output << ""
    end
    output
  end

  source = ["! version = 2.0", "! local concat = none", ""]
  ref = ["global", "var", "sub", "person", "array"]

  ref.each do |begin_type|
    next if deparsed["begin"][begin_type].nil? || deparsed["begin"][begin_type].empty?

    deparsed["begin"][begin_type].each do |key, value|
      if begin_type != "array"
        source << "! #{begin_type} #{key} = #{value}"
      else
        pipes = " "
        value.each do |test|
          if test.match?(/\s+/)
            pipes = "|"
            break
          end
        end
        source << "! #{begin_type} #{key} = #{value.join(pipes)}"
      end
    end
    source << ""
  end

  if deparsed["objects"]
    deparsed["objects"].each do |lang, lang_objects|
      next unless lang_objects && lang_objects["_objects"]

      sources = lang_objects["_sources"] || {}
      lang_objects["_objects"].each do |func, code|
        source << "> object #{func} #{lang}"
        if sources[func]
          source << sources[func].to_s.split("\n").map { |ln| "\t#{ln}" }.join("\n")
        elsif code.is_a?(String)
          body = code.to_s.match(/function[^{]+\{\n*([\s\S]*)\};?\s*$/m)
          source << body[1].strip.split("\n").map { |ln| "\t#{ln}" }.join("\n") if body
        elsif code.respond_to?(:source)
          # no-op for procs without source
        end
        source << "< object\n"
      end
    end
  end

  if deparsed["begin"]["triggers"] && deparsed["begin"]["triggers"].length > 0
    source << "> begin\n"
    source.concat(write_triggers.call(deparsed["begin"]["triggers"], "indent"))
    source << "< begin\n"
  end

  topics = deparsed["topics"].keys.sort
  topics.unshift("random")
  done_random = false

  topics.each do |topic_name|
    next unless deparsed["topics"].key?(topic_name)
    next if topic_name == "random" && done_random

    done_random = true if topic_name == "random"

    tagged = false
    tagline = []
    if topic_name != "random" ||
       (!(deparsed["inherits"][topic_name] || {}).empty? || !(deparsed["includes"][topic_name] || {}).empty?)
      tagged = true if topic_name != "random"

      inherits = (deparsed["inherits"][topic_name] || {}).keys
      includes = (deparsed["includes"][topic_name] || {}).keys

      if includes.length > 0
        tagline.concat(["includes"] + includes)
        tagged = true
      end
      if inherits.length > 0
        tagline.concat(["inherits"] + inherits)
        tagged = true
      end
    end

    if tagged
      source << ("> topic #{topic_name} " + tagline.join(" ")).strip + "\n"
    end

    source.concat(write_triggers.call(deparsed["topics"][topic_name], tagged))

    source << "< topic\n" if tagged
  end

  source.join("\n")
end

#warn(message, filename = nil, lineno = nil) ⇒ Object



28
29
30
# File 'lib/rivescript/parser.rb', line 28

def warn(message, filename = nil, lineno = nil)
  @master.warn(message, filename, lineno)
end