rcc.jj 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. options {
  2. STATIC=false;
  3. }
  4. PARSER_BEGIN(Rcc)
  5. /**
  6. * Licensed to the Apache Software Foundation (ASF) under one
  7. * or more contributor license agreements. See the NOTICE file
  8. * distributed with this work for additional information
  9. * regarding copyright ownership. The ASF licenses this file
  10. * to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance
  12. * with the License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. package com.yahoo.jute.compiler.generated;
  23. import com.yahoo.jute.compiler.*;
  24. import java.util.ArrayList;
  25. import java.util.Hashtable;
  26. import java.util.Iterator;
  27. import java.io.File;
  28. import java.io.FileReader;
  29. import java.io.FileNotFoundException;
  30. import java.io.IOException;
  31. public class Rcc {
  32. private static String language = "java";
  33. private static ArrayList recFiles = new ArrayList();
  34. private static JFile curFile;
  35. private static Hashtable recTab;
  36. private static String curDir = System.getProperty("user.dir");
  37. private static String curFileName;
  38. private static String curModuleName;
  39. public static void main(String args[]) {
  40. for (int i=0; i<args.length; i++) {
  41. if ("-l".equalsIgnoreCase(args[i]) ||
  42. "--language".equalsIgnoreCase(args[i])) {
  43. language = args[i+1].toLowerCase();
  44. i++;
  45. } else {
  46. recFiles.add(args[i]);
  47. }
  48. }
  49. if (!"c++".equals(language) && !"java".equals(language) && !"c".equals(language)) {
  50. System.out.println("Cannot recognize language:" + language);
  51. System.exit(1);
  52. }
  53. if (recFiles.size() == 0) {
  54. System.out.println("No record files specified. Exiting.");
  55. System.exit(1);
  56. }
  57. for (int i=0; i<recFiles.size(); i++) {
  58. curFileName = (String) recFiles.get(i);
  59. File file = new File(curDir, curFileName);
  60. try {
  61. FileReader reader = new FileReader(file);
  62. Rcc parser = new Rcc(reader);
  63. try {
  64. recTab = new Hashtable();
  65. curFile = parser.Input();
  66. System.out.println((String) recFiles.get(i) +
  67. " Parsed Successfully");
  68. } catch (ParseException e) {
  69. System.out.println(e.toString());
  70. System.exit(1);
  71. }
  72. try {
  73. reader.close();
  74. } catch (IOException e) {
  75. }
  76. } catch (FileNotFoundException e) {
  77. System.out.println("File " + (String) recFiles.get(i) +
  78. " Not found.");
  79. System.exit(1);
  80. }
  81. try {
  82. curFile.genCode(language);
  83. } catch (IOException e) {
  84. System.out.println(e.toString());
  85. System.exit(1);
  86. }
  87. }
  88. }
  89. }
  90. PARSER_END(Rcc)
  91. SKIP :
  92. {
  93. " "
  94. | "\t"
  95. | "\n"
  96. | "\r"
  97. }
  98. SPECIAL_TOKEN :
  99. {
  100. "//" : WithinOneLineComment
  101. }
  102. <WithinOneLineComment> SPECIAL_TOKEN :
  103. {
  104. <("\n" | "\r" | "\r\n" )> : DEFAULT
  105. }
  106. <WithinOneLineComment> MORE :
  107. {
  108. <~[]>
  109. }
  110. SPECIAL_TOKEN :
  111. {
  112. "/*" : WithinMultiLineComment
  113. }
  114. <WithinMultiLineComment> SPECIAL_TOKEN :
  115. {
  116. "*/" : DEFAULT
  117. }
  118. <WithinMultiLineComment> MORE :
  119. {
  120. <~[]>
  121. }
  122. TOKEN :
  123. {
  124. <MODULE_TKN: "module">
  125. | <RECORD_TKN: "class">
  126. | <INCLUDE_TKN: "include">
  127. | <BYTE_TKN: "byte">
  128. | <BOOLEAN_TKN: "boolean">
  129. | <INT_TKN: "int">
  130. | <LONG_TKN: "long">
  131. | <FLOAT_TKN: "float">
  132. | <DOUBLE_TKN: "double">
  133. | <USTRING_TKN: "ustring">
  134. | <BUFFER_TKN: "buffer">
  135. | <VECTOR_TKN: "vector">
  136. | <MAP_TKN: "map">
  137. | <LBRACE_TKN: "{">
  138. | <RBRACE_TKN: "}">
  139. | <LT_TKN: "<">
  140. | <GT_TKN: ">">
  141. | <SEMICOLON_TKN: ";">
  142. | <COMMA_TKN: ",">
  143. | <DOT_TKN: ".">
  144. | <CSTRING_TKN: "\"" ( ~["\""] )+ "\"">
  145. | <IDENT_TKN: ["A"-"Z","a"-"z"] (["a"-"z","A"-"Z","0"-"9","_"])*>
  146. }
  147. JFile Input() :
  148. {
  149. ArrayList ilist = new ArrayList();
  150. ArrayList rlist = new ArrayList();
  151. JFile i;
  152. ArrayList l;
  153. }
  154. {
  155. (
  156. i = Include()
  157. { ilist.add(i); }
  158. | l = Module()
  159. { rlist.addAll(l); }
  160. )+
  161. <EOF>
  162. { return new JFile(curFileName, ilist, rlist); }
  163. }
  164. JFile Include() :
  165. {
  166. String fname;
  167. Token t;
  168. }
  169. {
  170. <INCLUDE_TKN>
  171. t = <CSTRING_TKN>
  172. {
  173. JFile ret = null;
  174. fname = t.image.replaceAll("^\"", "").replaceAll("\"$","");
  175. File file = new File(curDir, fname);
  176. String tmpDir = curDir;
  177. String tmpFile = curFileName;
  178. curDir = file.getParent();
  179. curFileName = file.getName();
  180. try {
  181. FileReader reader = new FileReader(file);
  182. Rcc parser = new Rcc(reader);
  183. try {
  184. ret = parser.Input();
  185. System.out.println(fname + " Parsed Successfully");
  186. } catch (ParseException e) {
  187. System.out.println(e.toString());
  188. System.exit(1);
  189. }
  190. try {
  191. reader.close();
  192. } catch (IOException e) {
  193. }
  194. } catch (FileNotFoundException e) {
  195. System.out.println("File " + fname +
  196. " Not found.");
  197. System.exit(1);
  198. }
  199. curDir = tmpDir;
  200. curFileName = tmpFile;
  201. return ret;
  202. }
  203. }
  204. ArrayList Module() :
  205. {
  206. String mName;
  207. ArrayList rlist;
  208. }
  209. {
  210. <MODULE_TKN>
  211. mName = ModuleName()
  212. { curModuleName = mName; }
  213. <LBRACE_TKN>
  214. rlist = RecordList()
  215. <RBRACE_TKN>
  216. { return rlist; }
  217. }
  218. String ModuleName() :
  219. {
  220. String name = "";
  221. Token t;
  222. }
  223. {
  224. t = <IDENT_TKN>
  225. { name += t.image; }
  226. (
  227. <DOT_TKN>
  228. t = <IDENT_TKN>
  229. { name += "." + t.image; }
  230. )*
  231. { return name; }
  232. }
  233. ArrayList RecordList() :
  234. {
  235. ArrayList rlist = new ArrayList();
  236. JRecord r;
  237. }
  238. {
  239. (
  240. r = Record()
  241. { rlist.add(r); }
  242. )+
  243. { return rlist; }
  244. }
  245. JRecord Record() :
  246. {
  247. String rname;
  248. ArrayList flist = new ArrayList();
  249. Token t;
  250. JField f;
  251. }
  252. {
  253. <RECORD_TKN>
  254. t = <IDENT_TKN>
  255. { rname = t.image; }
  256. <LBRACE_TKN>
  257. (
  258. f = Field()
  259. { flist.add(f); }
  260. <SEMICOLON_TKN>
  261. )+
  262. <RBRACE_TKN>
  263. {
  264. String fqn = curModuleName + "." + rname;
  265. JRecord r = new JRecord(fqn, flist);
  266. recTab.put(fqn, r);
  267. return r;
  268. }
  269. }
  270. JField Field() :
  271. {
  272. JType jt;
  273. Token t;
  274. }
  275. {
  276. jt = Type()
  277. t = <IDENT_TKN>
  278. { return new JField(jt, t.image); }
  279. }
  280. JType Type() :
  281. {
  282. JType jt;
  283. Token t;
  284. String rname;
  285. }
  286. {
  287. jt = Map()
  288. { return jt; }
  289. | jt = Vector()
  290. { return jt; }
  291. | <BYTE_TKN>
  292. { return new JByte(); }
  293. | <BOOLEAN_TKN>
  294. { return new JBoolean(); }
  295. | <INT_TKN>
  296. { return new JInt(); }
  297. | <LONG_TKN>
  298. { return new JLong(); }
  299. | <FLOAT_TKN>
  300. { return new JFloat(); }
  301. | <DOUBLE_TKN>
  302. { return new JDouble(); }
  303. | <USTRING_TKN>
  304. { return new JString(); }
  305. | <BUFFER_TKN>
  306. { return new JBuffer(); }
  307. | rname = ModuleName()
  308. {
  309. if (rname.indexOf('.', 0) < 0) {
  310. rname = curModuleName + "." + rname;
  311. }
  312. JRecord r = (JRecord) recTab.get(rname);
  313. if (r == null) {
  314. System.out.println("Type " + rname + " not known. Exiting.");
  315. System.exit(1);
  316. }
  317. return r;
  318. }
  319. }
  320. JMap Map() :
  321. {
  322. JType jt1;
  323. JType jt2;
  324. }
  325. {
  326. <MAP_TKN>
  327. <LT_TKN>
  328. jt1 = Type()
  329. <COMMA_TKN>
  330. jt2 = Type()
  331. <GT_TKN>
  332. { return new JMap(jt1, jt2); }
  333. }
  334. JVector Vector() :
  335. {
  336. JType jt;
  337. }
  338. {
  339. <VECTOR_TKN>
  340. <LT_TKN>
  341. jt = Type()
  342. <GT_TKN>
  343. { return new JVector(jt); }
  344. }