Commit 645660ff authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Parse continuous signals and build valid AST

parent 4e740958
......@@ -12,8 +12,8 @@ continuous_signal
hyperlink?
PROVIDED expression e=end
(PRIORITY p=INT end)?
transition
-> ^(PROVIDED cif? hyperlink? expression $p? $e? transition)
transition?
-> ^(PROVIDED expression cif? hyperlink? $p? $e? transition?)
;
A new token ("PROVIDED") may be needed and must be added at several places
......@@ -23,7 +23,10 @@ in the ANTLR grammar:
- in the list of tokens (tokens { ... })
notes:
- "cif?" and "hyperlink?" are optional
- "cif" and "hyperlink" are optional
- the "transition" is also optional, to allow partial model saving
- the expression is always the first child, since it does not have a
dedicated token, and is the only mandatory field
- "end" corresponds to the COMMENT part
then the new production is added as a child option to the (existing) "state":
......@@ -93,6 +96,45 @@ class State(object):
self.continuous_signals = []
Then create the new class, using the same structure as another class of a
feature of the same family.
feature of the same family - here, we inherit from Input. The class is very
simple:
class ContinuousSignal(Input):
''' AST Entry for the Continuous Signal '''
def __init__(self):
''' Difference with Input: trigger is an expression '''
super(ContinuousSignal, self).__init__()
# Expression triggering the transition
self.trigger = None
# Priority (integer)
self.priority = 0
def trace(self):
''' Debug output for a Continuous signal '''
return u'PROVIDED {exp} ({l},{c})'.format(exp=self.inputString,
l=self.line, c=self.charPositionInLine)
We are done with the AST, we can close ogAST.py
4) Back to ogParser.py
Now we can parse the construct and create the AST entries we just defined.
In the state() function, we replace the placeholder we added at step 2:
(...)
elif child.type == lexer.PROVIDED:
# Continuous signal
provided_part, err, warn = continuous_signal(child, state_def,
context)
state_def.continuous_signals.append(provided_part)
warnings.extend(warn)
errors.extend(err)
Then we can implement the continuous_signal function.
def continuous_signal(root, parent, context):
''' Parse a PROVIDED clause in a continuous signal '''
i = ogAST.ContinuousSignal()
(...)
return i, errors, warnings
......@@ -2,7 +2,7 @@
 
# Resource object code
#
# Created: Sun Nov 22 08:45:14 2015
# Created: Sun Nov 22 17:13:10 2015
# by: The Resource Compiler for PySide (Qt v4.8.6)
#
# WARNING! All changes made in this file will be lost!
......@@ -27,7 +27,7 @@
See AdaGenerator.py for an example of use.
Copyright (c) 2012-2013 European Space Agency
Copyright (c) 2012-2015 European Space Agency
Designed and implemented by Maxime Perrotin
......@@ -611,6 +611,21 @@ class Connect(Input):
return u'CONNECT {exp} ({l},{c})'.format(exp=self.inputString,
l=self.line, c=self.charPositionInLine)
class ContinuousSignal(Input):
''' AST Entry for the Continuous Signal '''
def __init__(self):
''' Difference with Input: trigger is an expression '''
super(ContinuousSignal, self).__init__()
# Expression triggering the transition
self.trigger = None
# Priority (integer)
self.priority = 0
def trace(self):
''' Debug output for a Continuous signal '''
return u'PROVIDED {exp} ({l},{c})'.format(exp=self.inputString,
l=self.line, c=self.charPositionInLine)
class Start(object):
''' AST Entry for the START symbol '''
......
......@@ -2749,6 +2749,55 @@ def process_definition(root, parent=None, context=None):
return process, errors, warnings
def continuous_signal(root, parent, context):
''' Parse a PROVIDED clause in a continuous signal '''
i = ogAST.ContinuousSignal()
warnings, errors = [], []
# Keep track of the number of terminator statements in the transition
# useful if we want to render graphs from the SDL model
terminators = len(context.terminators)
i.trigger, err0, warn0 = expression(root.getChild(0), context)
for child in root.children[1:]:
if child.type == lexer.CIF:
# Get symbol coordinates
i.pos_x, i.pos_y, i.width, i.height = cif(child)
# # Report errors with symbol coordinates
# errors = [[e, [i.pos_x or 0, i.pos_y or 0], []] for e in errors]
# warnings = [[w, [i.pos_x or 0, i.pos_y or 0], []]
# for w in warnings]
elif child.type == lexer.INT:
# Priority
i.priority = int(child.text)
elif child.type == lexer.TRANSITION:
trans, err, warn = transition(child, parent=i, context=context)
errors.extend(err)
warnings.extend(warn)
i.transition = trans
# Associate a reference to the transition to the list of inputs
# The reference is an index to process.transitions table
context.transitions.append(trans)
i.transition_id = len(context.transitions) - 1
elif child.type == lexer.COMMENT:
i.comment, _, _ = end(child)
elif child.type == lexer.HYPERLINK:
i.hyperlink = child.getChild(0).toString()[1:-1]
elif child.type == 0:
# Syntax error caught by the parser, no need to report again
pass
else:
warnings.append('Unsupported INPUT child type: {}'
.format(child.type))
# At the end of the input parsing, get the the list of terminators that
# follow the input transition by making a diff with the list at process
# level (we counted the number of terminators before parsing the input)
i.terminators = list(context.terminators[terminators:])
return i, errors, warnings
def input_part(root, parent, context):
''' Parse an INPUT - set of TASTE provided interfaces '''
i = ogAST.Input()
......@@ -2958,7 +3007,12 @@ def state(root, parent, context):
elif child.type == lexer.HYPERLINK:
state_def.hyperlink = child.getChild(0).toString()[1:-1]
elif child.type == lexer.PROVIDED:
sterr.append('Continuous signals are not supported yet')
# Continuous signal
provided_part, err, warn = continuous_signal(child, state_def,
context)
state_def.continuous_signals.append(provided_part)
warnings.extend(warn)
errors.extend(err)
elif child.type == 0:
# Parser error, already caught
pass
......
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2015-11-22 08:45:16
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2015-11-22 17:13:13
import sys
from antlr3 import *
......
This diff is collapsed.
......@@ -627,8 +627,8 @@ continuous_signal
hyperlink?
PROVIDED expression e=end
(PRIORITY p=INT end)?
transition
-> ^(PROVIDED cif? hyperlink? expression $p? $e? transition)
transition?
-> ^(PROVIDED expression cif? hyperlink? $p? $e? transition?)
;
......
Markdown is supported
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