Class: Vangrail::Colang::Parser

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

Overview

Reads the Colang 1.0 subset that rail flows are written in.

define flow self check input
$allowed = execute self_check_input
if not $allowed
  bot refuse to respond
  stop

define bot refuse to respond
"I'm sorry, I can't respond to that."

That subset covers the input and output rails that ship with the toolkit and the ones people write. Dialog flows with user-intent matching are not supported, and a file using them raises rather than loading with the matching quietly missing: a guardrail that half-loads is a guardrail that reports checks it is not running.

Indentation defines blocks. Tabs are refused, because a file mixing tabs and spaces would otherwise parse into a different program than it looks.

Defined Under Namespace

Classes: Line

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename: nil) ⇒ Parser

Returns a new instance of Parser.



34
35
36
37
38
39
40
# File 'lib/vangrail/colang/parser.rb', line 34

def initialize(source, filename: nil)
  @source = source.to_s
  @filename = filename
  @flows = {}
  @bot_messages = {}
  @user_messages = {}
end

Class Method Details

.parse(source, filename: nil) ⇒ Object



30
31
32
# File 'lib/vangrail/colang/parser.rb', line 30

def self.parse(source, filename: nil)
  new(source, filename: filename).parse
end

Instance Method Details

#parseObject



42
43
44
45
46
47
# File 'lib/vangrail/colang/parser.rb', line 42

def parse
  lines = significant_lines
  index = 0
  index = definition(lines, index) while index < lines.length
  Program.new(flows: @flows, bot_messages: @bot_messages, user_messages: @user_messages)
end