# RiveScript-Python
#
# This code is released under the MIT License.
# See the "LICENSE" file for more information.
#
# https://www.rivescript.com/
from __future__ import unicode_literals
from .regexp import RE
import re
# Version of RiveScript we support.
rs_version = 2.0
class Parser(object):
"""The RiveScript language parser.
This module can be used as a stand-alone parser for third party developers
to use, if you want to be able to simply parse (and syntax check!)
RiveScript source code and get an "abstract syntax tree" back from it.
To that end, this module removed all dependencies on the parent RiveScript
class. When the RiveScript module uses this module, it passes its own debug
and warning functions as the ``on_debug`` and ``on_warn`` parameters, but
these parameters are completely optional.
Parameters:
strict (bool): Strict syntax checking (true by default).
utf8 (bool): Enable UTF-8 mode (false by default).
on_debug (func): An optional function to send debug messages to. If not
provided, you won't be able to get debug output from this module.
The debug function's prototype is: ``def f(message)``
on_warn (func): An optional function to send warning/error messages to.
If not provided, you won't be able to get any warnings from
this module. The warn function's prototype
is ``def f(message, filename='', lineno='')``
"""
# Concatenation mode characters.
concat_modes = dict(
none="",
space=" ",
newline="\n",
)
def __init__(self, strict=True, utf8=False, on_debug=None, on_warn=None):
self.strict = strict
self.utf8 = utf8
self.on_debug = on_debug
self.on_warn = on_warn
# Proxy functions
def say(self, *args, **kwargs):
if self.on_debug is not None:
self.on_debug(*args, **kwargs)
def warn(self, *args, **kwargs):
if self.on_warn is not None:
self.on_warn(*args, **kwargs)
def parse(self, filename, code):
"""Read and parse a RiveScript document.
Returns a data structure that represents all of the useful contents of
the document, in this format::
{
"begin": { # "begin" data
"global": {}, # map of !global vars
"var": {}, # bot !var's
"sub": {}, # !sub substitutions
"person": {}, # !person substitutions
"array": {}, # !array lists
},
"topics": { # main reply data
"random": { # (topic name)
"includes": {}, # map of included topics (values=1)
"inherits": {}, # map of inherited topics
"triggers": [ # array of triggers
{
"trigger": "hello bot",
"reply": [], # array of replies
"condition": [], # array of conditions
"redirect": None, # redirect command
"previous": None, # 'previous' reply
},
# ...
]
}
}
"objects": [ # parsed object macros
{
"name": "", # object name
"language": "", # programming language
"code": [], # array of lines of code
}
]
}
Args:
filename (str): The name of the file that the code came from, for
syntax error reporting purposes.
code (str[]): The source code to parse.
Returns:
dict: The aforementioned data structure.
"""
# Eventual returned structure ("abstract syntax tree" but not really)
ast = {
"begin": {
"global": {},
"var": {},
"sub": {},
"person": {},
"array": {},
},
"topics": {},
"objects": [],
}
# Track temporary variables.
topic = 'random' # Default topic=random
lineno = 0 # Line numbers for syntax tracking
comment = False # In a multi-line comment
inobj = False # In an object
objname = '' # The name of the object we're in
objlang = '' # The programming language of the object
objbuf = [] # Object contents buffer
curtrig = None # Pointer to the current trigger in ast.topics
isThat = None # Is a %Previous trigger
# Local (file scoped) parser options.
local_options = dict(
concat="none", # Concat mode for ^Continue command
)
# Read each line.
for lp, line in enumerate(code):
lineno += 1
self.say("Line: " + line + " (topic: " + topic + ") incomment: " + str(inobj))
if len(line.strip()) == 0: # Skip blank lines
continue
# In an object?
if inobj:
if re.match(RE.objend, line):
# End the object.
if len(objname):
ast["objects"].append({
"name": objname,
"language": objlang,
"code": objbuf,
})
objname = ''
objlang = ''
objbuf = []
inobj = False
else:
objbuf.append(line)
continue
line = line.strip() # Trim excess space. We do it down here so we
# don't mess up python objects!
line = RE.ws.sub(" ", line) # Replace the multiple whitespaces by single whitespace
# Look for comments.
if line[:2] == '//': # A single-line comment.
continue
elif line[0] == '#':
self.warn("Using the # symbol for comments is deprecated", filename, lineno)
elif line[:2] == '/*': # Start of a multi-line comment.
if '*/' not in line: # Cancel if the end is here too.
comment = True
continue
elif '*/' in line:
comment = False
continue
if comment:
continue
# Separate the command from the data.
if len(line) < 2:
self.warn("Weird single-character line '" + line + "' found.", filename, lineno)
continue
cmd = line[0]
line = line[1:].strip()
# Ignore inline comments if there's a space before the // symbols.
if " //" in line:
line = line.split(" //")[0].strip()
# Run a syntax check on this line.
syntax_error = self.check_syntax(cmd, line)
if syntax_error:
# There was a syntax error! Are we enforcing strict mode?
syntax_error = "Syntax error in " + filename + " line " + str(lineno) + ": " \
+ syntax_error + " (near: " + cmd + " " + line + ")"
if self.strict:
raise Exception(syntax_error)
else:
self.warn(syntax_error)
return # Don't try to continue
# Reset the %Previous state if this is a new +Trigger.
if cmd == '+':
isThat = None
# Do a lookahead for ^Continue and %Previous commands.
for i in range(lp + 1, len(code)):
lookahead = code[i].strip()
if len(lookahead) < 2:
continue
lookCmd = lookahead[0]
lookahead = lookahead[1:].strip()
# Only continue if the lookahead line has any data.
if len(lookahead) != 0:
# The lookahead command has to be either a % or a ^.
if lookCmd != '^' and lookCmd != '%':
break
# If the current command is a +, see if the following is
# a %.
if cmd == '+':
if lookCmd == '%':
isThat = lookahead
break
else:
isThat = None
# If the current command is a ! and the next command(s) are
# ^, we'll tack each extension on as a line break (which is
# useful information for arrays).
if cmd == '!':
if lookCmd == '^':
line += "