Skip to content

Commit

Permalink
Refactor listeners to BaseListener
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Jan 7, 2019
1 parent d84fb02 commit 848abb1
Show file tree
Hide file tree
Showing 7 changed files with 805 additions and 666 deletions.
27 changes: 4 additions & 23 deletions pydbc/dbcListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import re

import antlr4
from pydbc import parser

## Attributes
## GenSigStartValue ==> to set initial value until first message is received.
Expand Down Expand Up @@ -59,14 +59,9 @@ def extractAccessType(value):
else:
return None

class DbcListener(antlr4.ParseTreeListener):

class DbcListener(parser.BaseListener):

def getList(self, attr):
return [x for x in attr()] if attr() else []

def getTerminal(self, attr):
return attr().getText() if attr() else ''

def exitDbcfile(self, ctx):
self.value = dict(
Expand Down Expand Up @@ -289,7 +284,6 @@ def exitCustomAttributeDefaults(self, ctx):
defaults[name] = value
ctx.value = defaults


def exitCustomAttributeDefault(self, ctx):
name = ctx.n.value
value = ctx.v.value
Expand Down Expand Up @@ -331,6 +325,8 @@ def exitAttributeValueForObject(self, ctx):

def exitCustomAttributeValues(self, ctx):
print("CustomAttributeValues", ctx.items)
items = [x.value for x in ctx.items]
ctx.value = items

def exitCustomAttributeValueForObject(self, ctx):
print("customAttributeValueForObject", ctx.attrType.value)
Expand Down Expand Up @@ -370,18 +366,3 @@ def exitCategory(self, ctx):
di = dict(type = 'EV', envVarname = evName)
ctx.value = dict(category = category, **di)

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

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

def exitNumber(self, ctx):
ctx.value = ctx.i.value if ctx.i else ctx.f.value

def exitStringValue(self, ctx):
ctx.value = ctx.s.text.strip('"') if ctx.s else None

def exitIdentifierValue(self, ctx):
ctx.value = ctx.i.text if ctx.i else None

26 changes: 4 additions & 22 deletions pydbc/ldfListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__copyright__ = """
pySART - Simplified AUTOSAR-Toolkit for Python.
(C) 2010-2018 by Christoph Schueler <cpu12.gems.googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems.googlemail.com>
All Rights Reserved
Expand All @@ -29,16 +29,15 @@

from collections import namedtuple

import antlr4

class LdfListener(antlr4.ParseTreeListener):
from pydbc import parser

class LdfListener(parser.BaseListener):

def exitLin_description_file(self, ctx):
self.value = dict(
protocolVersion = ctx.pv.value,
languageVersion = ctx.lv.value,
fileRevision = ctx.fr.value if ctx.fr else None,
fileRevision = ctx.fr.value if ctx.fr else None,
speed = ctx.ls.value,
channelName = ctx.cn.value if ctx.cn else None,
nodes = ctx.ndef.value,
Expand Down Expand Up @@ -438,21 +437,4 @@ def exitSignal_representation_entry(self, ctx):
names = [x.value for x in ctx.names]
ctx.value = dict(name = enc, signalNames = names)

def exitIntValue(self, ctx):
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

def exitNumber(self, ctx):
ctx.value = ctx.i.value if ctx.i else ctx.f.value

def exitStringValue(self, ctx):
ctx.value = ctx.s.text.strip('"') if ctx.s else None

def exitIdentifierValue(self, ctx):
ctx.value = ctx.i.text if ctx.i else None

24 changes: 4 additions & 20 deletions pydbc/ncfListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__copyright__ = """
pySART - Simplified AUTOSAR-Toolkit for Python.
(C) 2010-2018 by Christoph Schueler <cpu12.gems.googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems.googlemail.com>
All Rights Reserved
Expand All @@ -28,9 +28,10 @@
__version__ = '0.1.0'


import antlr4
from pydbc import parser

class NcfListener(antlr4.ParseTreeListener):

class NcfListener(parser.BaseListener):

def exitToplevel(self, ctx):
v = ctx.v.value
Expand Down Expand Up @@ -243,21 +244,4 @@ def exitFree_text_definition(self, ctx):
text = ctx.f.value
ctx.value = text

def exitIntValue(self, ctx):
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

def exitNumber(self, ctx):
ctx.value = ctx.i.value if ctx.i else ctx.f.value

def exitStringValue(self, ctx):
ctx.value = ctx.s.text.strip('"') if ctx.s else None

def exitIdentifierValue(self, ctx):
ctx.value = ctx.i.text if ctx.i else None

5 changes: 4 additions & 1 deletion pydbc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def getTerminal(self, attr):
return attr().getText() if attr() else ''

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
18 changes: 18 additions & 0 deletions pydbc/py3/dbcListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,24 @@ def exitCustomAttributeValueForObject(self, ctx:dbcParser.CustomAttributeValueFo
pass


# Enter a parse tree produced by dbcParser#signalGroups.
def enterSignalGroups(self, ctx:dbcParser.SignalGroupsContext):
pass

# Exit a parse tree produced by dbcParser#signalGroups.
def exitSignalGroups(self, ctx:dbcParser.SignalGroupsContext):
pass


# Enter a parse tree produced by dbcParser#signalGroup.
def enterSignalGroup(self, ctx:dbcParser.SignalGroupContext):
pass

# Exit a parse tree produced by dbcParser#signalGroup.
def exitSignalGroup(self, ctx:dbcParser.SignalGroupContext):
pass


# Enter a parse tree produced by dbcParser#categoryDefinitions.
def enterCategoryDefinitions(self, ctx:dbcParser.CategoryDefinitionsContext):
pass
Expand Down
Loading

0 comments on commit 848abb1

Please sign in to comment.