123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- options {
- STATIC=false;
- }
- PARSER_BEGIN(Rcc)
- /**
- * 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.
- */
- package com.yahoo.jute.compiler.generated;
- import com.yahoo.jute.compiler.*;
- import java.util.ArrayList;
- import java.util.Hashtable;
- import java.util.Iterator;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class Rcc {
- private static String language = "java";
- private static ArrayList recFiles = new ArrayList();
- private static JFile curFile;
- private static Hashtable recTab;
- private static String curDir = System.getProperty("user.dir");
- private static String curFileName;
- private static String curModuleName;
- public static void main(String args[]) {
- for (int i=0; i<args.length; i++) {
- if ("-l".equalsIgnoreCase(args[i]) ||
- "--language".equalsIgnoreCase(args[i])) {
- language = args[i+1].toLowerCase();
- i++;
- } else {
- recFiles.add(args[i]);
- }
- }
- if (!"c++".equals(language) && !"java".equals(language) && !"c".equals(language)) {
- System.out.println("Cannot recognize language:" + language);
- System.exit(1);
- }
- if (recFiles.size() == 0) {
- System.out.println("No record files specified. Exiting.");
- System.exit(1);
- }
- for (int i=0; i<recFiles.size(); i++) {
- curFileName = (String) recFiles.get(i);
- File file = new File(curDir, curFileName);
- try {
- FileReader reader = new FileReader(file);
- Rcc parser = new Rcc(reader);
- try {
- recTab = new Hashtable();
- curFile = parser.Input();
- System.out.println((String) recFiles.get(i) +
- " Parsed Successfully");
- } catch (ParseException e) {
- System.out.println(e.toString());
- System.exit(1);
- }
- try {
- reader.close();
- } catch (IOException e) {
- }
- } catch (FileNotFoundException e) {
- System.out.println("File " + (String) recFiles.get(i) +
- " Not found.");
- System.exit(1);
- }
- try {
- curFile.genCode(language);
- } catch (IOException e) {
- System.out.println(e.toString());
- System.exit(1);
- }
- }
- }
- }
- PARSER_END(Rcc)
- SKIP :
- {
- " "
- | "\t"
- | "\n"
- | "\r"
- }
- SPECIAL_TOKEN :
- {
- "//" : WithinOneLineComment
- }
- <WithinOneLineComment> SPECIAL_TOKEN :
- {
- <("\n" | "\r" | "\r\n" )> : DEFAULT
- }
- <WithinOneLineComment> MORE :
- {
- <~[]>
- }
- SPECIAL_TOKEN :
- {
- "/*" : WithinMultiLineComment
- }
- <WithinMultiLineComment> SPECIAL_TOKEN :
- {
- "*/" : DEFAULT
- }
- <WithinMultiLineComment> MORE :
- {
- <~[]>
- }
- TOKEN :
- {
- <MODULE_TKN: "module">
- | <RECORD_TKN: "class">
- | <INCLUDE_TKN: "include">
- | <BYTE_TKN: "byte">
- | <BOOLEAN_TKN: "boolean">
- | <INT_TKN: "int">
- | <LONG_TKN: "long">
- | <FLOAT_TKN: "float">
- | <DOUBLE_TKN: "double">
- | <USTRING_TKN: "ustring">
- | <BUFFER_TKN: "buffer">
- | <VECTOR_TKN: "vector">
- | <MAP_TKN: "map">
- | <LBRACE_TKN: "{">
- | <RBRACE_TKN: "}">
- | <LT_TKN: "<">
- | <GT_TKN: ">">
- | <SEMICOLON_TKN: ";">
- | <COMMA_TKN: ",">
- | <DOT_TKN: ".">
- | <CSTRING_TKN: "\"" ( ~["\""] )+ "\"">
- | <IDENT_TKN: ["A"-"Z","a"-"z"] (["a"-"z","A"-"Z","0"-"9","_"])*>
- }
- JFile Input() :
- {
- ArrayList ilist = new ArrayList();
- ArrayList rlist = new ArrayList();
- JFile i;
- ArrayList l;
- }
- {
- (
- i = Include()
- { ilist.add(i); }
- | l = Module()
- { rlist.addAll(l); }
- )+
- <EOF>
- { return new JFile(curFileName, ilist, rlist); }
- }
- JFile Include() :
- {
- String fname;
- Token t;
- }
- {
- <INCLUDE_TKN>
- t = <CSTRING_TKN>
- {
- JFile ret = null;
- fname = t.image.replaceAll("^\"", "").replaceAll("\"$","");
- File file = new File(curDir, fname);
- String tmpDir = curDir;
- String tmpFile = curFileName;
- curDir = file.getParent();
- curFileName = file.getName();
- try {
- FileReader reader = new FileReader(file);
- Rcc parser = new Rcc(reader);
- try {
- ret = parser.Input();
- System.out.println(fname + " Parsed Successfully");
- } catch (ParseException e) {
- System.out.println(e.toString());
- System.exit(1);
- }
- try {
- reader.close();
- } catch (IOException e) {
- }
- } catch (FileNotFoundException e) {
- System.out.println("File " + fname +
- " Not found.");
- System.exit(1);
- }
- curDir = tmpDir;
- curFileName = tmpFile;
- return ret;
- }
- }
- ArrayList Module() :
- {
- String mName;
- ArrayList rlist;
- }
- {
- <MODULE_TKN>
- mName = ModuleName()
- { curModuleName = mName; }
- <LBRACE_TKN>
- rlist = RecordList()
- <RBRACE_TKN>
- { return rlist; }
- }
- String ModuleName() :
- {
- String name = "";
- Token t;
- }
- {
- t = <IDENT_TKN>
- { name += t.image; }
- (
- <DOT_TKN>
- t = <IDENT_TKN>
- { name += "." + t.image; }
- )*
- { return name; }
- }
- ArrayList RecordList() :
- {
- ArrayList rlist = new ArrayList();
- JRecord r;
- }
- {
- (
- r = Record()
- { rlist.add(r); }
- )+
- { return rlist; }
- }
- JRecord Record() :
- {
- String rname;
- ArrayList flist = new ArrayList();
- Token t;
- JField f;
- }
- {
- <RECORD_TKN>
- t = <IDENT_TKN>
- { rname = t.image; }
- <LBRACE_TKN>
- (
- f = Field()
- { flist.add(f); }
- <SEMICOLON_TKN>
- )+
- <RBRACE_TKN>
- {
- String fqn = curModuleName + "." + rname;
- JRecord r = new JRecord(fqn, flist);
- recTab.put(fqn, r);
- return r;
- }
- }
- JField Field() :
- {
- JType jt;
- Token t;
- }
- {
- jt = Type()
- t = <IDENT_TKN>
- { return new JField(jt, t.image); }
- }
- JType Type() :
- {
- JType jt;
- Token t;
- String rname;
- }
- {
- jt = Map()
- { return jt; }
- | jt = Vector()
- { return jt; }
- | <BYTE_TKN>
- { return new JByte(); }
- | <BOOLEAN_TKN>
- { return new JBoolean(); }
- | <INT_TKN>
- { return new JInt(); }
- | <LONG_TKN>
- { return new JLong(); }
- | <FLOAT_TKN>
- { return new JFloat(); }
- | <DOUBLE_TKN>
- { return new JDouble(); }
- | <USTRING_TKN>
- { return new JString(); }
- | <BUFFER_TKN>
- { return new JBuffer(); }
- | rname = ModuleName()
- {
- if (rname.indexOf('.', 0) < 0) {
- rname = curModuleName + "." + rname;
- }
- JRecord r = (JRecord) recTab.get(rname);
- if (r == null) {
- System.out.println("Type " + rname + " not known. Exiting.");
- System.exit(1);
- }
- return r;
- }
- }
- JMap Map() :
- {
- JType jt1;
- JType jt2;
- }
- {
- <MAP_TKN>
- <LT_TKN>
- jt1 = Type()
- <COMMA_TKN>
- jt2 = Type()
- <GT_TKN>
- { return new JMap(jt1, jt2); }
- }
- JVector Vector() :
- {
- JType jt;
- }
- {
- <VECTOR_TKN>
- <LT_TKN>
- jt = Type()
- <GT_TKN>
- { return new JVector(jt); }
- }
|