Skip to content

Commit 8d5e14c

Browse files
committed
Add local concat mode option support
Adds an option to enable RiveScript documents to override the concatenation character on a file-scoped basis. Valid examples are: ! local concat = none ! local concat = space ! local concat = newline The default is 'none'. This affects which character is used when a line has to be concatenated via the ^ command.
1 parent 5f2e457 commit 8d5e14c

7 files changed

Lines changed: 79 additions & 8 deletions

File tree

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Revision history for the Python package RiveScript.
1111
especially helpful was to precompile substitution and simple trigger
1212
regexps), taking the time-to-reply for the default brain from ~0.19s down
1313
to ~0.04s
14+
- Add support for `! local concat` option to override concatenation mode
15+
(file scoped)
1416

1517
1.06 Nov 25 2014
1618
- Change package name from python-rivescript to simply rivescript.

rivescript/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# SOFTWARE.
2626

2727
# Python 3 compat
28-
from __future__ import print_function
28+
from __future__ import print_function, unicode_literals
2929

3030
__author__ = 'Noah Petherbridge'
3131
__copyright__ = 'Copyright 2013, Noah Petherbridge'

rivescript/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
from __future__ import absolute_import
25+
from __future__ import absolute_import, unicode_literals
2626

2727
"""RiveScript's __main__.py
2828

rivescript/interactive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
from __future__ import print_function
25+
from __future__ import print_function, unicode_literals
2626

2727
"""interactive.py: RiveScript's built-in interactive mode.
2828

rivescript/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
# SOFTWARE.
2424

2525
# Python3 compat
26-
from __future__ import print_function
26+
from __future__ import print_function, unicode_literals
2727

2828
__docformat__ = 'plaintext'
2929

3030

31-
class PyRiveObjects:
31+
class PyRiveObjects(object):
3232
"""A RiveScript object handler for Python code.
3333
3434
This class provides built-in support for your RiveScript documents to include

rivescript/rivescript.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25+
from __future__ import unicode_literals
2526
import sys
2627
import os
2728
import re
@@ -78,9 +79,16 @@ class RE(object):
7879
RS_ERR_REPLY = "ERR: No Reply Found"
7980

8081

81-
class RiveScript:
82+
class RiveScript(object):
8283
"""A RiveScript interpreter for Python 2 and 3."""
8384

85+
# Concatenation mode characters.
86+
_concat_modes = dict(
87+
none="",
88+
space=" ",
89+
newline="\n",
90+
)
91+
8492
############################################################################
8593
# Initialization and Utility Methods #
8694
############################################################################
@@ -220,6 +228,11 @@ def _parse(self, fname, code):
220228
concnt = 0 # Condition counter
221229
isThat = '' # Is a %Previous trigger
222230

231+
# Local (file scoped) parser options.
232+
local_options = dict(
233+
concat="none", # Concat mode for ^Continue command
234+
)
235+
223236
# Read each line.
224237
for lp, line in enumerate(code):
225238
lineno = lineno + 1
@@ -329,7 +342,9 @@ def _parse(self, fname, code):
329342
# end of the current line.
330343
if cmd != '^' and lookCmd != '%':
331344
if lookCmd == '^':
332-
line += lookahead
345+
line += self._concat_modes.get(
346+
local_options["concat"], ""
347+
) + lookahead
333348
else:
334349
break
335350

@@ -372,7 +387,11 @@ def _parse(self, fname, code):
372387
continue
373388

374389
# Handle the rest of the types.
375-
if type == 'global':
390+
if type == 'local':
391+
# Local file-scoped parser options.
392+
self._say("\tSet parser option " + var + " = " + value)
393+
local_options[var] = value
394+
elif type == 'global':
376395
# 'Global' variables
377396
self._say("\tSet global " + var + " = " + value)
378397

tests/test_rivescript.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,56 @@ def test_topic_inheritence(self):
559559
self.reply("Say stuff.", RS_ERR_MATCH)
560560

561561

562+
class ParserOptionTest(RiveScriptTestCase):
563+
"""File scoped parser option tests."""
564+
565+
def test_concat(self):
566+
self.new("""
567+
// Default concat mode = none
568+
+ test concat default
569+
- Hello
570+
^ world!
571+
572+
! local concat = space
573+
+ test concat space
574+
- Hello
575+
^ world!
576+
577+
! local concat = none
578+
+ test concat none
579+
- Hello
580+
^ world!
581+
582+
! local concat = newline
583+
+ test concat newline
584+
- Hello
585+
^ world!
586+
587+
// invalid concat setting is equivalent to `none`
588+
! local concat = foobar
589+
+ test concat foobar
590+
- Hello
591+
^ world!
592+
593+
// the option is file scoped so it can be left at
594+
// any setting and won't affect subsequent parses
595+
! local concat = newline
596+
""")
597+
self.extend("""
598+
// concat mode should be restored to the default in a
599+
// separate file/stream parse
600+
+ test concat second file
601+
- Hello
602+
^ world!
603+
""")
604+
605+
self.reply("test concat default", "Helloworld!")
606+
self.reply("test concat space", "Hello world!")
607+
self.reply("test concat none", "Helloworld!")
608+
self.reply("test concat newline", "Hello\nworld!")
609+
self.reply("test concat second file", "Helloworld!")
610+
611+
562612
class UnicodeTest(RiveScriptTestCase):
563613
"""UTF-8 Tests."""
564614

0 commit comments

Comments
 (0)