#!/bin/bash
## Licensed to the Apache Software Foundation (ASF) under one
## or more contributor license agreements.  See the NOTICE file
## distributed with this work for additional information
## regarding copyright ownership.  The ASF licenses this file
## to you under the Apache License, Version 2.0 (the
## "License"); you may not use this file except in compliance
## with the License.  You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

# Parser builder

CLASS=Turtle
DIR="../src/main/java/com/hp/hpl/jena/n3/turtle/parser"
FILE=turtle.jj
NAME=Turtle

(cd "$DIR" ; rm TokenMgrError.java ParseException.java Token.java JavaCharStream.java )

echo "---- Process grammar ----"
## No it's now!  echo "Warning about UNICODE_INPUT is ignorable"
javacc -OUTPUT_DIRECTORY=$DIR -JDK_VERSION=1.5 "$FILE"
RC=$?
[ "$RC" = 0 ] || return
echo "---- Create text form ----"
jjdoc -TEXT=true -OUTPUT_FILE=${FILE%%.jj}.txt "${FILE}"

## echo "---- Fixing Java warnings ---- "
## 
## 
##     F="$DIR/${CLASS}ParserTokenManager.java"
##     sed -e 's/import .*//' -e 's/MatchLoop: do/do/' < $F > F
##     mv F $F
##     rm -f F

 # Fix unnecessary imports
    echo "---- Fixing Java warnings in ${NAME}TokenManager ..."

    F="$DIR/${CLASS}ParserTokenManager.java"

    sed -e 's/import .*//' -e 's/MatchLoop: do/do/' \
        -e 's/int hiByte = (int)(curChar/int hiByte = (curChar/' \
	< $F > F
    mv F $F

    echo "---- Fixing Java warnings in ParseException ..."
    #Override:
    #   public String getMessage()
    F="$DIR/ParseException.java"
    sed -e 's/@Override //' \
	-e 's/public String getMessage/@Override public String getMessage/' < $F > F
    mv F $F

    echo "---- Fixing Java warnings in Token ..."
    F="$DIR/Token.java"
    sed -e 's/@Override //' \
	-e 's/public String toString/@Override public String toString/' < $F > F
    mv F $F

    echo "---- Fixing Java warnings in TokenMgrError ..."
    # Override:
    #   public String getMessage()
    F="$DIR/TokenMgrError.java"
    sed -e 's/@Override //' \
	-e 's/public String getMessage/@Override public String getMessage/' < $F > F
    mv F $F

    echo "---- Fixing Java warnings in ${CLASS}Parser ..."
    F="$DIR/${CLASS}Parser.java"
    sed -e 's/for (java.util.Iterator/for (java.util.Iterator<int[]>/' \
	-e 's/(int\[\])//' \
	-e 's/^\s*;\s*$//' \
        < $F > F
    mv F $F

    echo "---- Done"
