From 0419693b04b930fc1f17127a29588e4401f63eb2 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 24 Jul 2019 05:26:14 +0200 Subject: [PATCH] initial commit: test grammars + tooling --- .gitignore | 4 ++++ README.md | 3 +++ build.sh | 44 ++++++++++++++++++++++++++++++++++ test-grammars/graph-this.sh | 46 ++++++++++++++++++++++++++++++++++++ test-grammars/tokens | 11 +++++++++ test-grammars/tokens.example | 4 ++++ 6 files changed, 112 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 build.sh create mode 100755 test-grammars/graph-this.sh create mode 100644 test-grammars/tokens create mode 100644 test-grammars/tokens.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca02e8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +test-grammars/*.json +test-grammars/*.dot +test-grammars/*.pdf +test-grammars/*.png diff --git a/README.md b/README.md new file mode 100644 index 0000000..33023bb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# recipies parser + +Parsing grammars requires the `pegasus` software: https://github.com/DanilaFe/pegasus diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..b5105cd --- /dev/null +++ b/build.sh @@ -0,0 +1,44 @@ +#/bin/sh + +BINDIR=../pegasus/bin/ + +tokenfile=$1 +TMPERRORFILE="/tmp/TMPERRORFILE$$" + +on_error() +{ + if [ $? -ne 0 ]; then + err=$1 + echo $err + cat ${TMPERRORFILE} + rm ${TMPERRORFILE} + exit 1 + fi + val=`wc -l ${TMPERRORFILE} | cut -d ' ' -f 1` + if [ ${val} -ne 0 ]; then + echo $err + cat ${TMPERRORFILE} + rm ${TMPERRORFILE} + exit 1 + fi +} + +if [ $# -eq 0 ] +then + echo "usage $0 token-and-rules-file" + exit 1 +fi + +echo "pegasus reading the grammar" +${BINDIR}/pegasus < "${tokenfile}" > "${tokenfile}.json" 2>${TMPERRORFILE} +on_error "cannot read the grammar" + +echo "pegasus creating a dot file" +${BINDIR}/pegasus-dot < "${tokenfile}.json" > "${tokenfile}.dot" +# echo VAL RET $? +on_error "cannot create the dot file" + +echo "pegasus trying an example on the generated parser" +cat "${tokenfile}.example" | ${BINDIR}/pegasus-sim -i "${tokenfile}.json" +# echo VAL RET $? +on_error "cannot parse the example" diff --git a/test-grammars/graph-this.sh b/test-grammars/graph-this.sh new file mode 100755 index 0000000..5752b68 --- /dev/null +++ b/test-grammars/graph-this.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +if [ "$FONT" = "" ] +then + FONT=/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf +fi + +echo "font : $FONT" + +# $1 = program, $2 = filename extension +function graphit() +{ + PROG=$1 + FNEXT=$2 + ls *.$FNEXT 2>/dev/null 1>&2 + if [ $? -eq 0 ]; then + for i in *\.$FNEXT + do + PDF=$(echo ${i} | sed "s/$FNEXT$/pdf/") + if [ ! -f ${PDF} ] || [ $(stat -c "%X" ${PDF}) -lt $(stat -c "%X" ${i}) ] + then + + PROGOPT="-Tpdf" + case $PROG in + "seqdiag" | "packetdiag" | "nwdiag") + PROGOPT="$PROGOPT -a -f $FONT" + echo ${PROG} ${PROGOPT} ${i} + ${PROG} ${PROGOPT} ${i} + ;; + "dot") + echo "${PROG} ${PROGOPT} ${i} > ${PDF}" + ${PROG} ${PROGOPT} ${i} > ${PDF} + ;; + esac + + echo touch ${PDF} + touch ${PDF} + fi + done + fi +} + +graphit "seqdiag" "diag" +graphit "packetdiag" "pktdiag" +graphit "nwdiag" "nwdiag" +graphit "dot" "dot" diff --git a/test-grammars/tokens b/test-grammars/tokens new file mode 100644 index 0000000..a6b357d --- /dev/null +++ b/test-grammars/tokens @@ -0,0 +1,11 @@ +token cr = /\n/; +token eq = /=/; +token space = /[ \t]+/; +token recipiename = /@[a-zA-Z0-9\.\-]+/; +token actionname = /(build|version|depends)/; +token actionvalue = /"[a-zA-Z0-9 \-_]+"/; + +rule document = recipe cr document | recipe | cr; +rule recipe = recipiename cr myrules; +rule myrules = myrule cr myrules | cr; +rule myrule = actionname eq actionvalue; diff --git a/test-grammars/tokens.example b/test-grammars/tokens.example new file mode 100644 index 0000000..bf8493e --- /dev/null +++ b/test-grammars/tokens.example @@ -0,0 +1,4 @@ +@test1234 +build="the thing to do" +version="1" +depends="nothing"