## defs/RPSLSyntax.java

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. RPSLSyntax
2. getSyntaxOfs
3. getSyntaxByName
4. escapeStringForC
5. writeAttributeInfo
6. writeSyntaxTab
7. writeSyntaxMakefile
8. main

       1 /******************
       2   Copyright (c) 2002                                        RIPE NCC
       3
       4   All Rights Reserved
       5
       6   Permission to use, copy, modify, and distribute this software and its
       7   documentation for any purpose and without fee is hereby granted,
       8   provided that the above copyright notice appear in all copies and that
       9   both that copyright notice and this permission notice appear in
      10   supporting documentation, and that the name of the author not be
      11   used in advertising or publicity pertaining to distribution of the
      12   software without specific, written prior permission.
      13
      14   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      15   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      16   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      17   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      18   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      19   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      20   ***************************************/
      21
      22 import javax.xml.parsers.*;
      23 import org.w3c.dom.*;
      24 import org.xml.sax.*;
      25
      26 import java.util.*;
      27 import java.io.*;
      28
      29 public class RPSLSyntax {
         /* [<][>][^][v][top][bottom][index][help] */
      30     private Vector attributeSyntaxes;
      31
      32     // constructor - call with an XML rpsl_syntax node
      33     public RPSLSyntax(Node node)
      34     {
      35         attributeSyntaxes = new Vector();
      36
      37         // load values from the XML node
      38         Node kid = node.getFirstChild();
      39         while (kid != null)
      40         {
      41             String nodeName = kid.getNodeName();
      42             String nodeValue = kid.getNodeValue();
      43             if (nodeName.equals("attribute_syntax"))
      44             {
      45                 attributeSyntaxes.add(new AttributeSyntax(kid));
      46             }
      47             kid = kid.getNextSibling();
      48         }
      49     }
      50
      51     public int getSyntaxOfs(String name)
         /* [<][>][^][v][top][bottom][index][help] */
      52     {
      53         for (int i=0; i<attributeSyntaxes.size(); i++) {
      54             AttributeSyntax as=(AttributeSyntax)attributeSyntaxes.elementAt(i);
      55             if (as.getName().equals(name)) {
      56                 return i;
      57             }
      58         }
      59         return -1;
      60     }
      61
      62     public AttributeSyntax getSyntaxByName(String name)
         /* [<][>][^][v][top][bottom][index][help] */
      63     {
      64         for (int i=0; i<attributeSyntaxes.size(); i++) {
      65             AttributeSyntax as=(AttributeSyntax)attributeSyntaxes.elementAt(i);
      66             if (as.getName().equals(name)) {
      67                 return as;
      68             }
      69         }
      70         return null;
      71     }
      72
      73     private String escapeStringForC(String s)
         /* [<][>][^][v][top][bottom][index][help] */
      74     {
      75         StringBuffer sb = new StringBuffer();
      76
      77         byte[] bytes = s.getBytes();
      78
      79         for (int i=0; i<bytes.length; i++) {
      80             byte c = bytes[i];
      81             if (c == '\\') {
      82                 sb.append("\\\\");
      83             } else if (c == '"') {
      84                 sb.append("\\\"");
      85             } else if ((c <= 0x1F) || (c >= 0x7F)) {
      86                 if (c < 0x10) {
      87                     sb.append("\\x0");
      88                 } else {
      89                     sb.append("\\x");
      90                 }
      91                 Byte b = new Byte(c);
      92                 sb.append(Integer.toHexString(b.intValue()));
      93             } else {
      94                 sb.append(s.substring(i, i+1));
      95             }
      96         }
      97         return sb.toString();
      98     }
      99
     100     private void writeAttributeInfo(PrintWriter out, AttributeSyntax a)
         /* [<][>][^][v][top][bottom][index][help] */
     101     {
     102         out.println("        /* name */");
     103         out.println("        \"" + a.getName() + "\",");
     104         out.println("        /* core_regex_pattern */");
     105         String coreRegex = a.getCoreRegex();
     106         if (coreRegex == null) {
     107             out.println("        NULL,");
     108         } else {
     109             out.println("        \"" + escapeStringForC(coreRegex.trim()) +
     110                         "\",");
     111         }
     112         out.println("        /* core_regex, set by syntax_init() */");
     113         out.println("        NULL,");
     114         out.println("        /* core_reserved_regex_pattern */");
     115         String coreReservedRegex = a.getCoreReservedRegex();
     116         if (coreReservedRegex == null) {
     117             out.println("        NULL,");
     118         } else {
     119             out.println("        \"" +
     120                         escapeStringForC(coreReservedRegex.trim()) + "\",");
     121         }
     122         out.println("        /* core_reserved_regex, set by syntax_init() */");
     123         out.println("        NULL,");
     124         String coreParserName = a.getCoreParserName();
     125         out.println("        /* core_parser_reset */");
     126         if (coreParserName == null) {
     127             out.println("        NULL,");
     128         } else {
     129             out.println("        " + coreParserName + "_reset,");
     130         }
     131         out.println("        /* core_parser */");
     132         if (coreParserName == null) {
     133             out.println("        NULL,");
     134         } else {
     135             out.println("        " + coreParserName + "parse,");
     136         }
     137         out.println("        /* front_end_regex_pattern */");
     138         String regex = a.getFrontEndRegex();
     139         if (regex == null) {
     140             regex = coreRegex;
     141         }
     142         if (regex == null) {
     143             out.println("        NULL,");
     144         } else {
     145             out.println("        \""+escapeStringForC(regex.trim())+"\",");
     146         }
     147         out.println("        /* front_end_regex, set by syntax_init() */");
     148         out.println("        NULL,");
     149         out.println("        /* front_end_reserved_regex_pattern */");
     150         regex = a.getFrontEndReservedRegex();
     151         if (regex == null) {
     152             regex = coreReservedRegex;
     153         }
     154         if (regex == null) {
     155             out.println("        NULL,");
     156         } else {
     157             out.println("        \""+escapeStringForC(regex.trim())+"\",");
     158         }
     159         out.println("        /* front_end_reserved_regex, set by syntax_init() */");
     160         out.println("        NULL,");
     161         String parser_name = a.getFrontEndParserName();
     162         if (parser_name == null) {
     163             parser_name = coreParserName;
     164         }
     165         out.println("        /* front_end_parser_reset */");
     166         if (parser_name == null) {
     167             out.println("        NULL,");
     168         } else {
     169             out.println("        " + parser_name + "_reset,");
     170         }
     171         out.println("        /* front_end_parser */");
     172         if (parser_name == null) {
     173             out.println("        NULL ");
     174         } else {
     175             out.println("        " + parser_name + "parse");
     176         }
     177     }
     178
     179     public void writeSyntaxTab(String fileName) throws IOException
         /* [<][>][^][v][top][bottom][index][help] */
     180     {
     181         PrintWriter out = new PrintWriter(new FileWriter(fileName));
     182         out.println("/* generated by 'RPSLSyntax.java' - DO NOT HAND-EDIT */");
     183         out.println("#ifndef SYNTAX_TAB_H");
     184         out.println("#define SYNTAX_TAB_H");
     185         out.println();
     186
     187         Enumeration e = attributeSyntaxes.elements();
     188         while (e.hasMoreElements()) {
     189             AttributeSyntax a = (AttributeSyntax)e.nextElement();
     190             String parserName = a.getCoreParserName();
     191             if (parserName != null) {
     192                 out.println("extern void " + parserName + "_reset();");
     193                 out.println("extern int " + parserName + "parse();");
     194             }
     195             parserName = a.getFrontEndParserName();
     196             if (parserName != null) {
     197                 out.println("extern void " + parserName + "_reset();");
     198                 out.println("extern int " + parserName + "parse();");
     199             }
     200         }
     201
     202         out.println();
     203         out.println("syntax_t syntax_tab[] = {");
     204
     205         if (!attributeSyntaxes.isEmpty()) {
     206             out.println("    {");
     207             e = attributeSyntaxes.elements();
     208             writeAttributeInfo(out, (AttributeSyntax)e.nextElement());
     209             while (e.hasMoreElements()) {
     210                 out.println("    },");
     211                 out.println("    {");
     212                 writeAttributeInfo(out, (AttributeSyntax)e.nextElement());
     213             }
     214             out.println("    }");
     215         }
     216
     217         out.println("};");
     218         out.println();
     219         out.println("#endif /* SYNTAX_TAB_H */");
     220         out.close();
     221     }
     222
     223     public void writeSyntaxMakefile(String fileName) throws IOException
         /* [<][>][^][v][top][bottom][index][help] */
     224     {
     225         PrintWriter out = new PrintWriter(new FileWriter(fileName));
     226         out.println("# generated by 'RPSLSyntax.java' - DO NOT HAND-EDIT");
     227         out.println();
     228
     229         out.println("all: $(RIPLIBDIR)/librpsl.a $(RIPINCDIR)/syntax_api.h");
     230         out.println();
     231
     232         out.println("$(RIPINCDIR)/syntax_api.h: syntax_api.h");
     233         out.println("\trm -f $(RIPINCDIR)/syntax_api.h");
     234         out.println("\tln -s syntax_api.h $(RIPINCDIR)/syntax_api.h");
     235         out.println();
     236
     237         int numOfParser = 0;
     238         Enumeration e = attributeSyntaxes.elements();
     239         while (e.hasMoreElements()) {
     240             AttributeSyntax a = (AttributeSyntax)e.nextElement();
     241             if (a.getCoreParserName() != null) {
     242                 numOfParser++;
     243             }
     244             if (a.getFrontEndParserName() != null) {
     245                 numOfParser++;
     246             }
     247         }
     248
     249         if (numOfParser == 0) {
     250             out.println("$(RIPLIBDIR)/librpsl.a: " +
     251                         "syntax_api.o class.o attribute.o syntax.o");
     252         } else {
     253             out.println("$(RIPLIBDIR)/librpsl.a: " +
     254                         "syntax_api.o class.o attribute.o syntax.o\\");
     255             e = attributeSyntaxes.elements();
     256             int parsersLeft = numOfParser;
     257             while (parsersLeft > 0) {
     258                 AttributeSyntax a = (AttributeSyntax)e.nextElement();
     259                 String parserName = a.getCoreParserName();
     260                 if (parserName != null) {
     261                     out.print("        " + parserName + ".lex.o " +
     262                                            parserName + ".tab.o");
     263                     if (parsersLeft > 1) {
     264                         out.println(" \\");
     265                     } else {
     266                         out.println();
     267                     }
     268                     parsersLeft--;
     269                 }
     270                 parserName = a.getFrontEndParserName();
     271                 if (parserName != null) {
     272                     out.print("        " + parserName + ".lex.o " +
     273                                            parserName + ".tab.o");
     274                     if (parsersLeft > 1) {
     275                         out.println(" \\");
     276                     } else {
     277                         out.println();
     278                     }
     279                     parsersLeft--;
     280                 }
     281             }
     282         }
     283         out.println("\tar -r $(RIPLIBDIR)/librpsl.a $?");
     284         out.println("\tar -r $(RIPLIBDIR)/librip.a $?");
     285         out.println();
     286
     287         e = attributeSyntaxes.elements();
     288         while (e.hasMoreElements()) {
     289             AttributeSyntax a = (AttributeSyntax)e.nextElement();
     290             String parserName = a.getCoreParserName();
     291             if (parserName != null) {
     292                 out.println(parserName + ".lex.o: " + parserName + ".lex.c " +
     293                                                       parserName + ".tab.h");
     294                 out.println("\t$(CC) $(CFLAGS) -c " + parserName + ".lex.c");
     295                 out.println();
     296
     297                 out.println(parserName + ".tab.o: " + parserName + ".tab.c " +
     298                                                       parserName + ".tab.h");
     299                 out.println("\t$(CC) $(CFLAGS) -c " + parserName + ".tab.c");
     300                 out.println();
     301
     302                 out.println(parserName + ".lex.c: " + parserName + ".l");
     303                 out.println("\tflex -i -P" + parserName +
     304                             " -o" + parserName + ".lex.c " +
     305                             parserName + ".l");
     306                 out.println();
     307
     308                 out.println(parserName + ".tab.c " + parserName + ".tab.h: " +
     309                             parserName + ".y");
     310                 out.println("\tbison -d -p" + parserName + " " +
     311                             parserName + ".y");
     312                 out.println();
     313             }
     314             parserName = a.getFrontEndParserName();
     315             if (parserName != null) {
     316                 out.println(parserName + ".lex.o: " + parserName + ".lex.c " +
     317                                                       parserName + ".tab.h");
     318                 out.println("\t$(CC) $(CFLAGS) -c " + parserName + ".lex.c");
     319                 out.println();
     320
     321                 out.println(parserName + ".tab.o: " + parserName + ".tab.c " +
     322                                                       parserName + ".tab.h");
     323                 out.println("\t$(CC) $(CFLAGS) -c " + parserName + ".tab.c");
     324                 out.println();
     325
     326                 out.println(parserName + ".lex.c: " + parserName + ".l");
     327                 out.println("\tflex -i -P" + parserName +
     328                             " -o" + parserName + ".lex.c " +
     329                             parserName + ".l");
     330                 out.println();
     331
     332                 out.println(parserName + ".tab.c " + parserName + ".tab.h: " +
     333                             parserName + ".y");
     334                 out.println("\tbison -d -p" + parserName + " " +
     335                             parserName + ".y");
     336                 out.println();
     337             }
     338         }
     339         out.close();
     340     }
     341
     342     // when run as an application, produce the "syntax_tab.h" header, as
     343     // well as the Makefile.syntax to actually compile everything
     344     public static void main(String[] args) throws ParserConfigurationException,
         /* [<][>][^][v][top][bottom][index][help] */
     345                                                   SAXException,
     346                                                   IOException
     347     {
     348         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     349         DocumentBuilder db = dbf.newDocumentBuilder();
     350         Document doc = db.parse(new File("syntax.xml"));
     351         RPSLSyntax syntaxes = null;
     352         Node syntax_search = doc.getFirstChild();
     353         while (syntax_search != null) {
     354             if (syntax_search.getNodeName().equals("rpsl_syntax")) {
     355                 syntaxes = new RPSLSyntax(syntax_search);
     356             }
     357             syntax_search = syntax_search.getNextSibling();
     358         }
     359         syntaxes.writeSyntaxTab("syntax_tab.h");
     360         syntaxes.writeSyntaxMakefile("Makefile.syntax");
     361     }
     362 }
     363

/* [<][>][^][v][top][bottom][index][help] */