示例#1
0
  public static Node parseFile(File file, Grammar grammar, int start) {

    try {
      Parser parser = new Parser(grammar, start);
      Tokenizer tokenizer = new Tokenizer(file);
      Token tok = tokenizer.nextToken();
      int colOffset = 1;
      int lineNo = 1;
      while (parser.addToken(tok, colOffset) != ReturnCode.ACCEPT) {

        tok = tokenizer.nextToken();
        if (tok.state != TokState.NEWLINE
            && tok.state != TokState.INDENT
            && tok.state != TokState.DEDENT) {
          if (tok.lineNo != lineNo) {
            colOffset = 1;
            lineNo = tok.lineNo;
          } else {
            colOffset++;
          }
        }
      }
      return parser.tree;
    } catch (PyExceptions e) {
      e.printStackTrace();
      throw e;
    }
  }
示例#2
0
 public static void main(String[] args) {
   File file = new File("./test/test.py");
   try {
     Tokenizer tokenizer = new Tokenizer(file);
     Token tok = tokenizer.nextToken();
     while (tok.state != TokState.ENDMARKER) {
       System.out.println(tok);
       tok = tokenizer.nextToken();
     }
   } catch (PyExceptions e) {
     e.printStackTrace();
   }
 }