Skip to content

Commit

Permalink
- added json file parser
Browse files Browse the repository at this point in the history
- added json parser test case
  • Loading branch information
LudwigCRON committed Feb 10, 2019
1 parent 6493369 commit f2c612a
Show file tree
Hide file tree
Showing 12 changed files with 1,552 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode
.DS_Store
.antlr
__pycache__

59 changes: 59 additions & 0 deletions lib/JSONMLGrammar.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

grammar JSONMLGrammar;

jsonml
: hashmap
| array
;

hashmap
: '{' pair (',' pair)* '}'
| '{' '}'
;

pair
: STRING ':' value
;

array
: '[' value (',' value)* ']'
| '[' ']'
;

value
: STRING
| NUMBER
| hashmap
| array
| 'true'
| 'false'
| 'null'
;


STRING
: '"' (ESC | ~ ["\\])* '"'
;
fragment ESC
: '\\' (["\\/bfnrt] | UNICODE)
;
fragment UNICODE
: 'u' HEX HEX HEX HEX
;
fragment HEX
: [0-9a-fA-F]
;
NUMBER
: '-'? INT '.' [0-9] + EXP? | '-'? INT EXP | '-'? INT
;
fragment INT
: '0' | [1-9] [0-9]*
;
// no leading zeros
fragment EXP
: [Ee] [+\-]? INT
;
// \- since - means "range" inside [...]
WS
: [ \t\n\r] + -> skip
;
14 changes: 14 additions & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ANTLR for file format check and parsing
PROJECTLIB_PATH=$(shell pwd)
ANTLR_JAR=${PROJECTLIB_PATH}/antlr-4.7.1-complete.jar
export CLASSPATH=.:${ANTLR_JAR}
ANTLR4=java -Xmx500M -cp "${ANTLR_JAR}:${CLASSPATH}" org.antlr.v4.Tool
GRUN=java org.antlr.v4.runtime.misc.TestRig
WORKDIR=./antlr_jsonml

test:
#rm -r ${WORKDIR}
mkdir -p ${WORKDIR}
cp JSONMLGrammar.g4 ${WORKDIR}/JSONMLGrammar.g4
cd ${WORKDIR}; ${ANTLR4} -Dlanguage=Python3 JSONMLGrammar.g4
cd ${WORKDIR}; ${GRUN} JSONMLGrammar jsonml -tokens
Binary file added lib/antlr-4.7.1-complete.jar
Binary file not shown.
Empty file removed pywave.py
Empty file.
4 changes: 4 additions & 0 deletions pywave/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env python3

from .bricks import BRICKS, Brick, ANALOG_CONTEXT
from .renderer import Renderer, SvgRenderer
28 changes: 18 additions & 10 deletions pywave/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"""

import re
import skin
from .skin import DEFAULT, DEFINITION
from itertools import count
from bricks import BRICKS, Brick, generate_brick
from .bricks import BRICKS, Brick, generate_brick

# Counter for unique id generation
# counter of group of wave
Expand Down Expand Up @@ -189,6 +189,14 @@ def _reduce_wavelane(self, name: str, wavelane: str, **kwargs):
previous_brick = b
return _wavelane

def _get_or_eval(self, name: str, default, **kwargs):
"""
if is a str, evaluate the code or get it in a standard way
"""
if isinstance(kwargs.get(name), str):
return eval(kwargs.get(name, ""))
return kwargs.get(name, default)

@incr_wavelane
def wavelane(self, name: str, wavelane: str, extra: str = "", **kwargs):
"""
Expand All @@ -209,15 +217,15 @@ def wavelane(self, name: str, wavelane: str, extra: str = "", **kwargs):
"""
# options
period = kwargs.get("period", 1)
periods = kwargs.get("periods", [])
phase = kwargs.get("phase", 0)
data = kwargs.get("data", "")
brick_width = period * kwargs.get("brick_width", 20)
brick_height = kwargs.get("brick_height", 20) * kwargs.get("vscale", 1)
gap_offset = kwargs.get("gap_offset", brick_width*0.75)
slewing = kwargs.get("slewing", 3)
duty_cycles = kwargs.get("duty_cycles", [])
analogue = kwargs.get("analogue", [])
analogue = self._get_or_eval("analogue", [], **kwargs)
duty_cycles = self._get_or_eval("duty_cycles", [], **kwargs)
periods = self._get_or_eval("periods", [], **kwargs)
# in case a string is given reformat it as a list
if isinstance(data, str):
data = data.strip().split()
Expand Down Expand Up @@ -282,7 +290,7 @@ def wavelane(self, name: str, wavelane: str, extra: str = "", **kwargs):
symbol,
generate_brick(symbol, **kwargs),
(self.translate(pos, 0) +
f"class=\"s{b if b.isdigit() and int(b, 10) > 1 else ''}\"")
f"class=\"s{b if b.isdigit() and int(b, 10) > 1 else '2'}\"")
))
if symbol == BRICKS.data:
data_counter += 1
Expand All @@ -303,7 +311,6 @@ def _gen():
ans = self.wavelane_title(name, vscale=kwargs.get("vscale", 1)) if name else ""
for w in wave:
symb, b, e = w
print(name, symb, e)
ans += self.brick(symb, b, extra=e)
return ans
return self.group(
Expand Down Expand Up @@ -521,7 +528,8 @@ def size(self, wavelanes, brick_width: int = 20, brick_height: int = 28, depth:
if not "periods" in wavelanes[wavetitle]:
_l = len(wavelanes[wavetitle]["wave"])
else:
_l = sum(wavelanes[wavetitle]["periods"])
periods = self._get_or_eval("periods", [], **wavelanes[wavetitle])
_l = sum(periods)
_l *= brick_width
_l *= wavelanes[wavetitle].get("repeat", 1)
_l *= wavelanes[wavetitle].get("period", 1)
Expand Down Expand Up @@ -633,8 +641,8 @@ def draw(self, wavelanes, **kwargs) -> str:
return (
f"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width+lkeys*11}\" height=\"{height}\" "
f"viewBox=\"-1 -1 {width+lkeys*11+2} {height+2}\" overflow=\"hidden\">\n"
f"<style>{skin.DEFAULT}</style>\n"
f"{skin.DEFINITION}"
f"<style>{DEFAULT}</style>\n"
f"{DEFINITION}"
""+self.wavegroup(_id, wavelanes, brick_width=brick_width, brick_height=brick_height, width=width, height=height)[1]+""
"\n</svg>"
)
2 changes: 1 addition & 1 deletion pywave/skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
.h4{font-size:9.38px;font-weight:bold}
.h5{font-size:7.50px;font-weight:bold}
.h6{font-size:6px;font-weight:bold}
.path{fill:none;
.s0, .s1, .path{fill:none;
stroke:#000;
stroke-width:1;
stroke-linecap:round;
Expand Down

0 comments on commit f2c612a

Please sign in to comment.