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. A tab anywhere in the indent run is refused, because a file mixing tabs and spaces would otherwise parse into a different program than it looks.

Assignments, if conditions, and action arguments share one value grammar: string, int, and bool literals, $vars, execute, not, and == / !=.

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.



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

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



36
37
38
# File 'lib/vangrail/colang/parser.rb', line 36

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

Instance Method Details

#parseObject



48
49
50
51
52
53
# File 'lib/vangrail/colang/parser.rb', line 48

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