Commit e89c0a61 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Augment syntax with syntactic sugar for observers

parent 8c9e9341
......@@ -124,6 +124,9 @@ The background pattern was downloaded from www.subtlepatterns.com
Changelog
=========
**3.5.0 (04/2021)**
- Support Input/Ouput expressions for model checkers
**3.4.6 (04/2021)**
- Introduce monitors to support model checking observers
......
This diff is collapsed.
......@@ -1629,6 +1629,70 @@ def unary_expression(root, context):
return expr, errors, warnings
def io_expression(root, context):
''' Expressions used in the context of observers (for model checking):
input
input x [from P] to F
output
output X from P
Since this is syntactic sugar, we transform these expression into the
regular form based on the known structure of events: Observable_Event
type that is generated by kazoo.
'''
inputString = get_input_string(root)
event_kind = "{kind}_event"
target_option = " and then event.{kind}_event.{target} = {function}"
msg_name = " and then present(event.{kind}_event.event.{function}.msg_{direction}) = {msg}"
string = "present(event) = "
msg, src, dest = "", "", ""
if root.type == lexer.INPUT_EXPRESSION:
kind = "input"
direction = "in"
else:
kind = "output"
direction = "out"
string += event_kind.format(kind=kind)
for child in root.getChildren():
if child.type == lexer.ID:
msg = child.text
elif child.type == lexer.FROM:
src = child.getChild(0).text
string += target_option.format(kind=kind,
target="source",
function=src)
elif child.type == lexer.TO:
dest = child.getChild(0).text
string += target_option.format(kind=kind,
target="dest",
function=dest)
else:
raise NotImplementedError("In io_expression")
if msg:
string += msg_name.format(kind=kind,
function=dest if kind=="input" else src,
direction=direction,
msg=msg)
parser = parser_init (string=string)
new_root = parser.expression()
tree = new_root.tree
tree.token_stream = parser.getTokenStream()
expr, errors, warnings = expression(tree, context)
expr.inputString = inputString
return expr, errors, warnings
def expression(root, context, pos='right'):
''' Expression analysis (e.g. 5+5*hello(world)!foo) '''
logic = (lexer.OR, lexer.AND, lexer.XOR, lexer.IMPLIES)
......@@ -1677,6 +1741,10 @@ def expression(root, context, pos='right'):
# If the procedure is not defined with a return value, a TypeError
# has been raised in compare_type, so no need to check it again here
return prim, errs, warns
elif root.type == lexer.INPUT_EXPRESSION:
return io_expression(root, context)
elif root.type == lexer.OUTPUT_EXPRESSION:
return io_expression(root, context)
else:
raise NotImplementedError(sdl92Parser.tokenNamesMap[root.type] +
' - line ' + str(root.getLine()))
......@@ -5811,7 +5879,7 @@ def parseSingleElement(elem:str='', string:str='', context=None):
# so we have to discard exceptions sent by e.g. find_variable
pass
except NotImplementedError as err:
syntax_errors.append('Syntax error in expression - Fix it.')
syntax_errors.append('Expression syntax not supported.')
except SyntaxError as err:
syntax_errors.append(err.text)
# Check that the whole string has been consumed (ANTLR may stop parsing
......
......@@ -141,7 +141,7 @@ except ImportError:
__all__ = ['opengeode', 'SDL_Scene', 'SDL_View', 'parse']
__version__ = '3.4.6'
__version__ = '3.5.0'
if hasattr(sys, 'frozen'):
# Detect if we are running on Windows (py2exe-generated)
......
This diff is collapsed.
This diff is collapsed.
......@@ -1407,8 +1407,16 @@ class ContinuousSignal(HorizontalSymbol):
_terminal_followers = ['Join', 'State', 'ProcedureStop']
common_name = 'continuous_signal'
# Define reserved keywords for the syntax highlighter
blackbold = SDL_BLACKBOLD
redbold = SDL_REDBOLD
blackbold = SDL_BLACKBOLD.copy()
redbold = SDL_REDBOLD.copy()
redbold.remove("\\bINPUT\\b")
redbold.remove("\\bOUTPUT\\b")
redbold.remove("\\bSTATE\\b")
blackbold.append("\\bINPUT\\b")
blackbold.append("\\bOUTPUT\\b")
blackbold.append("\\bAND\\b")
blackbold.append("\\bFROM\\b")
blackbold.append("\\bTO\\b")
def __init__(self, parent=None, ast=None):
''' Create the Provided symbol - use no background color '''
......
......@@ -138,6 +138,8 @@ tokens {
VARIABLES;
VIA;
VIAPATH;
INPUT_EXPRESSION;
OUTPUT_EXPRESSION;
}
......@@ -1089,6 +1091,8 @@ unary_expression
| NOT^ unary_expression
| DASH unary_expression -> ^(NEG unary_expression)
| CALL procedure_call_body -> ^(PROCEDURE_CALL procedure_call_body)
| input_expression // used in observers
| output_expression // used in observers
;
......@@ -1101,6 +1105,23 @@ postfix_expression
)+
;
// input and output expression allow observers (for model checking) to
// monitor the sending and receiving of messages with a nice syntax
// (e.g. event = output msg from foo)
input_expression
: INPUT
-> ^(INPUT_EXPRESSION)
| INPUT (msg=ID)? (FROM src=ID)? TO dest=ID
-> ^(INPUT_EXPRESSION $msg? ^(FROM $src)? ^(TO $dest))
;
output_expression
: OUTPUT
-> ^(OUTPUT_EXPRESSION)
| OUTPUT (msg=ID)? (FROM src=ID) (TO dest=ID)?
-> ^(OUTPUT_EXPRESSION $msg? ^(FROM $src) ^(TO $dest)?)
;
primary_expression
: primary -> ^(PRIMARY primary)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment