parser: migrate prescc to Perl
authorJean Privat <jean@pryen.org>
Wed, 3 Apr 2013 17:42:29 +0000 (13:42 -0400)
committerJean Privat <jean@pryen.org>
Wed, 3 Apr 2013 17:42:29 +0000 (13:42 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/parser/Makefile
src/parser/README
src/parser/prescc.pl [moved from src/parser/prescc.sh with 66% similarity]

index bdb1379..e7d1fa8 100644 (file)
@@ -20,7 +20,7 @@ all: parser.nit
 
 # Preprocess the extended sablecc gramar
 .nit.sablecc3: nit.sablecc3xx
-       ./prescc.sh $< $@
+       ./prescc.pl $< > $@
 
 # Note that parser_nodes is no more automatically synced with the grammar
 parser.nit: .nit.sablecc3 xss/*.xss
index 227bcfc..981578a 100644 (file)
@@ -5,8 +5,8 @@ Contents:
 
        fact_parser.pl: Script used to factorize parser.nit
        Makefile: Update grammar and generate .nit files
-       nit.sablecc3xx: Extended sablecc3 grammar (see prescc.sh)
-       prescc.sh: Program to transform an extended sablecc3 to a standard one
+       nit.sablecc3xx: Extended sablecc3 grammar (see prescc.pl)
+       prescc.pl: Program to transform an extended sablecc3 to a standard one
        parser_nodes.nit: token and nodes classes hierarchy used by the parser and the lexer
        tables.nit, tables_nit.h: Interfaces to access the tables needed by the parser and the lexer
        test_parser.nit:
similarity index 66%
rename from src/parser/prescc.sh
rename to src/parser/prescc.pl
index e5ab277..4159cf8 100755 (executable)
@@ -1,3 +1,4 @@
+#!/usr/bin/perl -w
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
 # Copyright 2009 Jean Privat <jean@pryen.org>
@@ -31,7 +32,7 @@
 #
 # Limitations
 #
-#   prescc is badly implemented with shell, sed and perl and is not robust.
+#   prescc is badly implemented in perl and is not robust.
 #   Users must remember the following:
 #    * parametrized productions MUST be terminated with a line containing only a single semicolon (;)
 #    * parameters (~) and guards (!) in alternatives MUST correspond to a parameter of the enclosing production
 #                nop
 #                ;
 
+while (<>) {
+       push @lines, $_;
+}
+$lines = join "", @lines;
 
-case $# in
-       2);;
-       *) echo "Usage: prescc infile outfile"; exit
-esac
+# List all the available parameters in the extended grammar
+@params = ();
+while ($lines =~ /\~([a-zA-Z]+)/g) {
+       if (!$found{$1}) {
+               push @params, $1;
+               $found{$1}=1;
+       }
+}
 
-
-infile=$1
-outfile=$2
-tmpfile=`mktemp "$2.XXXXXX"`
-
-printf "/* This file is autogenerated, do not modify it */" > "$outfile"
-cat "$infile" >> "$outfile"
-
-# The perl code is used to list all the available parameters in the extended grammar
-for token in `perl -ne 'while (/\~([a-zA-Z]+)/g) {if (!$found{$1}) {print "$1\n"; $found{$1}=1}}' "$infile"`
-do
-       echo "Parameter ~$token"
-       # first, sed starts from first line to the AST line and removes ~xxx and !xxx
-       sed -n -e "
-               1,/^Abstract Syntax Tree/{
-                       /^Abstract Syntax Tree/b
-                       s/[\~!]$token//g
-                       p
-               }
-       " "$outfile" > "$tmpfile"
+$ast = "Abstract Syntax Tree";
+@res = ();
+for $token (@params) {
+       print STDERR "Parameter ~$token\n";
+       #push @res, "//Start part $token\n";
+# first, sed starts from first line to the AST line and removes ~xxx and !xxx
+       for $l (@lines) {
+               $_ = $l;
+               last if (/^$ast/);
+               s/[~!]$token//g;
+               push @res, $_;
+       }
+       #push @res, "//Generated part $token\n";
        # second, sed clones ~xxx parametrized productions, substitute ~xxx with _xxx and delete !xxx lines
-       sed -n -e "
-               /\~$token/,/;/{
-                       s/\~$token/_$token/g
-                       /!$token/d
-                       p
-               }
-       " "$outfile" >> "$tmpfile"
-       # third, sed continues fron AST line to last line and remove ~xxx and !xxx
-       sed -n -e "
-               /^Abstract Syntax Tree/,\${
-                       s/[\~!]$token//g
-                       p
-               }
-       " "$outfile" >> "$tmpfile"
-       mv "$tmpfile" "$outfile"
-done
+       $into = 0;
+       for $l (@lines) {
+               $_ = $l;
+               $into = 1 if (/~$token/);
+               next if (!$into);
+               s/~$token/_$token/g;
+               next if /!$token/;
+               push @res, $_;
+               $into = 0 if (/;/);
+       }
+       #push @res, "//End of generated part $token\n";
 
+       # third, sed continues fron AST line to last line and remove ~xxx and !xxx
+       $into = 0;
+       for (@lines) {
+               $into = 1 if (/^$ast/);
+               next if (!$into);
+               push @res, $_;
+       }
+       #push @res, "//End part $token\n";
+       @lines = @res;
+       @res = ();
+}
+print "/* This file is autogenerated, do not modify it */";
+print (join "", @lines);