parser: add a basic sabblecc3 parser in Java
[nit.git] / src / parser / org / nitlanguage / gen / TestParser.java
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * This file is free software, which comes along with NIT. This software is
4 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 * is kept unaltered, and a notification of the changes is added.
8 * You are allowed to redistribute it and sell it, alone or is a part of
9 * another product.
10 */
11
12 package org.nitlanguage.gen;
13
14 import java.io.*;
15 import java.nio.file.*;
16
17 import org.nitlanguage.gen.lexer.*;
18 import org.nitlanguage.gen.parser.*;
19 import org.nitlanguage.gen.analysis.*;
20 import org.nitlanguage.gen.node.*;
21
22 public class TestParser extends DepthFirstAdapter {
23 public static void main(String[] args) throws Exception {
24 if (args.length == 0) {
25 BufferedReader ir = new BufferedReader(new InputStreamReader(System.in));
26 work(ir);
27 } else {
28 for(int i=0; i<args.length; i++) {
29 //System.out.println(args[i]);
30
31 //Here we use "ISO-8859-1" because we want to opaquely treat Bytes as characters.
32 //Unfortunately "US-ASCII" does not seems to do the job for values>127.
33 BufferedReader ir = new BufferedReader(new InputStreamReader(new FileInputStream(args[i]), "ISO-8859-1"));
34
35 work(ir);
36 }
37 }
38 }
39
40 public static void work(BufferedReader ir) {
41 TestParser testParser = new TestParser();
42 try {
43 PushbackReader r = new PushbackReader(ir, 1024);
44 Parser parser = new Parser(new Lexer(r));
45 Node syntaxTree = parser.parse();
46 syntaxTree.apply(testParser);
47 } catch (LexerException e) {
48 System.out.println(e.getMessage() + ".");
49 } catch (ParserException e) {
50 System.out.println(e.getMessage() + ".");
51 } catch (IOException e) {
52 System.out.println(e.getMessage() + ".");
53 System.exit(1);
54 }
55 }
56 }