Skip to content

Commit

Permalink
Update grammar stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Dec 28, 2018
1 parent 758552e commit bd1fe23
Show file tree
Hide file tree
Showing 6 changed files with 1,599 additions and 1,367 deletions.
6 changes: 5 additions & 1 deletion pydbc/ldfListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def exitSupplier_id(self, ctx):

def exitFunction_id(self, ctx):
ctx.value = ctx.i.value
print("function_id:", ctx.value)

def exitVariant(self, ctx):
ctx.value = ctx.i.value
Expand Down Expand Up @@ -418,7 +419,10 @@ def exitSignal_representation_entry(self, ctx):
ctx.value = OrderedDict(name = enc, signalNames = names)

def exitIntValue(self, ctx):
ctx.value = int(ctx.i.text) if ctx.i else None
if ctx.i:
ctx.value = int(ctx.i.text, 10)
elif ctx.h:
ctx.value = int(ctx.h.text, 16)

def exitFloatValue(self, ctx):
ctx.value = float(ctx.f.text) if ctx.f else None
Expand Down
2 changes: 1 addition & 1 deletion pydbc/ncf.g4
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ bitrate:
diagnostic_definition:
'diagnostic' '{'
'NAD' '=' //(nads += intValue (',' nads += intValue)*) | (nads += intValue 'to' nads += intValue) ';'
lhs = intValue ('to' rhs = intValue) | (',' nads += intValue)* ';'
lhs = intValue (('to' rhs = intValue) | (',' nads += intValue)*) ';'
'diagnostic_class' '=' dc = intValue ';'
('P2_min' '=' p2Min = number 'ms' ';')?
('ST_min' '=' stMin = number 'ms' ';')?
Expand Down
132 changes: 100 additions & 32 deletions pydbc/ncfListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@
class NcfListener(antlr4.ParseTreeListener):

def exitToplevel(self, ctx):
self. value = "hello"
v = ctx.v.value
nodes = [x.value for x in ctx.nodes]
self.value = dict(version = v, nodes = nodes)

def exitLanguage_version(self, ctx):
print("language_ver", ctx.s.value)
s = ctx.s.value
ctx.value = s

def exitNode_definition(self, ctx):
print("node_def", ctx.name.value, ctx.g.value, ctx.d.value, ctx.f.value)
name = ctx.name.value
g = ctx.g.value
d = ctx.d.value
f = ctx.f.value
e = ctx.e.value
s = ctx.s.value
t = ctx.t.value if ctx.t else None
ctx.value = dict(name = name, general = g, diagnostic = d, frames= f, encodings = e, status = s, freeText = t)

def exitNode_name(self, ctx):
print("node_name", ctx.i.value)
ctx.value = ctx.i.value

def exitGeneral_definition(self, ctx):
print("gen_def", ctx.pv.value)
pv = ctx.pv.value
sup = ctx.sup.value
fun = ctx.fun.value
var = ctx.var.value
br = ctx.br.value
tf = True if ctx.tf.text == "yes" else False
ctx.value = dict(protocolVersion = pv, supplier = sup, function = fun, variant = var, bitrate = br, wakeupSignal = tf)

def exitProtocol_version(self, ctx):
ctx.value = ctx.s.value
Expand All @@ -33,58 +48,97 @@ def exitVariant_id(self, ctx):
ctx.value = ctx.i.value

def exitBitrate_definition(self, ctx):
pass
rates = br = minBr = maxBr = None
if ctx.rates:
rates = [x.value for x in ctx.rates]
elif ctx.br:
br = ctx.br.value
else:
minBr = ctx.minBr.value
maxBr = ctx.maxBr.value
ctx.value = dict(bitrate = br, minBr = minBr, maxBr = maxBr, rates = rates)

def exitBitrate(self, ctx):
pass
ctx.value = ctx.n.value

def exitDiagnostic_definition(self, ctx):
pass
lhs = ctx.lhs.value
rhs = ctx.rhs.value if ctx.rhs else None
nads = [x.value for x in ctx.nads] if ctx.nads else []
dc = ctx.dc.value if ctx.dc else None
p2Min = ctx.p2Min.value if ctx.p2Min else None
stMin = ctx.stMin.value if ctx.p2Min else None
nAs = ctx.nAs.value if ctx.nAs else None
nCr = ctx.nCr.value if ctx.nCr else None
sids = [x.value for x in ctx.sids] if ctx.sids else []
mml = ctx.mml.value if ctx.mml else None
ctx.value = dict(
maxMessageLength = mml, lhs = lhs, rhs = rhs, nads = nads, diagnosticClass = dc, p2Min = p2Min, stMin = stMin, nAs = nAs, nCr = nCr, supportedSids = sids
)

def exitFrame_definition(self, ctx):
print("frame_def", ctx.frames)
frames = [x.value for x in ctx.frames]
ctx.value = frames

def exitSingle_frame(self, ctx):
print("single_frame", ctx.d)
n = ctx.n.value
p = ctx.p.value
s = ctx.s.value if ctx.s else None
ctx.value = dict(name = n, properties = p, signal = s)

def exitFrame_kind(self, ctx):
print("frame_kind")
text = ctx.v.text
ctx.value = text

def exitFrame_name(self, ctx):
print("frame_name")
name = ctx.i.value
ctx.value = name

def exitFrame_properties(self, ctx):
pass
"""
'length' '=' l = intValue ';'
('min_period' '=' minValue = intValue 'ms' ';')?
('max_period' '=' maxValue = intValue 'ms' ';')?
('event_triggered_frame' '=' etf = identifierValue)?
"""

def exitSignal_definition(self, ctx):
ctx.value = [x.value for x in ctx.items]

def exitSignal_definition(self, ctx):
n = ctx.n.value
p = ctx.p.value
ctx.value = OrderedDict(name = n, properties = p)
ctx.value = dict(name = n, properties = p)

def exitSignal_name(self, ctx):
pass
name = ctx.i.value
ctx.value = name

def exitSignal_properties(self, ctx):
pass
init = ctx.init.value
s = ctx.s.value
o = ctx.o.value
e = ctx.e.value if ctx.e else None
ctx.value = dic(initValue = init, size = s, offset = o, encoding = e)

def exitInit_value(self, ctx):
pass
scalar = ctx.s.value if ctx.s else None
array = ctx.a.value if ctx.a else None
ctx.value = OrderedDict(scalar = scalar, array = array)

def exitInit_value_scalar(self, ctx):
pass
ctx.value = ctx.i.value

def exitInit_value_array(self, ctx):
pass
ctx.value = [x.value for x in ctx.vs]

def exitEncoding_definition(self, ctx):
ctx.value = [x.value for x in ctx.items]

def exitEncoding_definition_entry(self, ctx):
name = ctx.name.value
items = [x.value for x in ctx.items]
ctx.value = dict(name = name, values = items)

def exitEncoding_definition_value(self, ctx):
if ctx.l:
Expand All @@ -99,16 +153,24 @@ def exitEncoding_definition_value(self, ctx):
elif ctx.a:
value = ctx.a.name
vtype = "ascii"
ctx.value = OrderedDict(value = value, valueType = vtype)
ctx.value = dict(value = value, valueType = vtype)

def exitEncoding_name(self, ctx):
pass
ctx.value = ctx.i.value

def exitLogical_value(self, ctx):
pass
s = ctx.s.value
t = ctx.t.value if ctx.t else None
ctx.value = dict(signal = s, text = t)

def exitPhysical_range(self, ctx):
pass
#'physical_value' ',' minValue = min_value ',' maxValue = max_value ',' s = scale ',' o = offset (',' t = text_info)? ';'
minValue = ctx.minValue.value
maxValue = ctx.maxValue.value
s = ctx.s.value
o = ctx.o.value
t = ctx.t.value if ctx.t else None
ctx.value = dict(min = minValue, max = maxValue, scale = s, offset = o, text = t)

def exitBcd_value(self, ctx):
pass
Expand All @@ -117,34 +179,40 @@ def exitAscii_value(self, ctx):
pass

def exitSignal_value(self, ctx):
pass
ctx.value = ctx.n.value

def exitMin_value(self, ctx):
pass
ctx.value = ctx.n.value

def exitMax_value(self, ctx):
pass
ctx.value = ctx.n.value

def exitScale(self, ctx):
pass
ctx.value = ctx.n.value

def exitOffset(self, ctx):
pass
ctx.value = ctx.n.value

def exitText_info(self, ctx):
pass
ctx.value = ctx.t.value

def exitStatus_management(self, ctx):
pass
r = ctx.r.value
values = [x.value for x in ctx.values] if ctx.values else []
ctx.value = dict(responseError = r, faultStateSignals = values)

def exitPublished_signal(self, ctx):
pass

def exitFree_text_definition(self, ctx):
pass
text = ctx.f.value
ctx.value = text

def exitIntValue(self, ctx):
ctx.value = int(ctx.i.text) if ctx.i else None
if ctx.i:
ctx.value = int(ctx.i.text, 10)
elif ctx.h:
ctx.value = int(ctx.h.text, 16)

def exitFloatValue(self, ctx):
ctx.value = float(ctx.f.text) if ctx.f else None
Expand Down
Loading

0 comments on commit bd1fe23

Please sign in to comment.