initial commit: test grammars + tooling

master
Philippe PITTOLI 2019-07-24 05:26:14 +02:00
commit 0419693b04
6 changed files with 112 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
test-grammars/*.json
test-grammars/*.dot
test-grammars/*.pdf
test-grammars/*.png

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# recipies parser
Parsing grammars requires the `pegasus` software: https://github.com/DanilaFe/pegasus

44
build.sh Executable file
View File

@ -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"

46
test-grammars/graph-this.sh Executable file
View File

@ -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"

11
test-grammars/tokens Normal file
View File

@ -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;

View File

@ -0,0 +1,4 @@
@test1234
build="the thing to do"
version="1"
depends="nothing"