示例#1
0
 private void reportError(String message, CommonTree ast) {
   // Print an error message relating to the given
   // (sub)AST.
   int line = ast.getLine(), column = ast.getCharPositionInLine();
   System.err.println("line " + line + ":" + column + " " + message);
   errorCount++;
 }
  public WhileStatement(CommonTree MyTree, Model MyModel) {
    setLine(MyTree.getLine());
    this.MyModel = MyModel;

    Comparison = ExpressionFactory.getExpression((CommonTree) MyTree.getChild(0), MyModel);
    if (MyTree.getChildCount() > 1) {
      for (int i = 1; i < MyTree.getChildCount(); i++)
        Statements.add(StatementFactory.getStatement((CommonTree) MyTree.getChild(i), MyModel));
    }
  }
示例#3
0
文件: DefRef.java 项目: chuzui/antlr
  // $ANTLR start "assignment"
  // DefRef.g:107:1: assignment : ^(eq= '=' member . ) ;
  public final void assignment() throws RecognitionException {
    CommonTree eq = null;
    Type member6 = null;

    try {
      // DefRef.g:108:5: ( ^(eq= '=' member . ) )
      // DefRef.g:108:9: ^(eq= '=' member . )
      {
        eq = (CommonTree) match(input, 27, FOLLOW_27_in_assignment473);
        if (state.failed) return;

        match(input, Token.DOWN, null);
        if (state.failed) return;
        pushFollow(FOLLOW_member_in_assignment475);
        member6 = member();

        state._fsp--;
        if (state.failed) return;
        matchAny(input);
        if (state.failed) return;

        match(input, Token.UP, null);
        if (state.failed) return;
        if (state.backtracking == 1) {

          System.out.println("line " + eq.getLine() + ": assign to type " + member6.getName());
        }
      }

    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return;
  }
示例#4
0
文件: DefRef.java 项目: chuzui/antlr
  // $ANTLR start "member"
  // DefRef.g:118:1: member returns [Type type] : ( ^( '.' m= member ID ) | ID );
  public final Type member() throws RecognitionException {
    Type type = null;

    CommonTree ID7 = null;
    CommonTree ID8 = null;
    Type m = null;

    try {
      // DefRef.g:119:5: ( ^( '.' m= member ID ) | ID )
      int alt6 = 2;
      int LA6_0 = input.LA(1);

      if ((LA6_0 == 30)) {
        alt6 = 1;
      } else if ((LA6_0 == ID)) {
        alt6 = 2;
      } else {
        if (state.backtracking > 0) {
          state.failed = true;
          return type;
        }
        NoViableAltException nvae = new NoViableAltException("", 6, 0, input);

        throw nvae;
      }
      switch (alt6) {
        case 1:
          // DefRef.g:119:7: ^( '.' m= member ID )
          {
            match(input, 30, FOLLOW_30_in_member527);
            if (state.failed) return type;

            match(input, Token.DOWN, null);
            if (state.failed) return type;
            pushFollow(FOLLOW_member_in_member531);
            m = member();

            state._fsp--;
            if (state.failed) return type;
            ID7 = (CommonTree) match(input, ID, FOLLOW_ID_in_member533);
            if (state.failed) return type;

            match(input, Token.UP, null);
            if (state.failed) return type;
            if (state.backtracking == 1) {

              StructSymbol scope = (StructSymbol) m; // get scope of expr
              Symbol s =
                  scope.resolveMember((ID7 != null ? ID7.getText() : null)); // resolve ID in scope
              System.out.println(
                  "line "
                      + ID7.getLine()
                      + ": ref "
                      + m.getName()
                      + "."
                      + (ID7 != null ? ID7.getText() : null)
                      + "="
                      + s);
              if (s != null) type = s.type; // return ID's type
            }
          }
          break;
        case 2:
          // DefRef.g:127:7: ID
          {
            ID8 = (CommonTree) match(input, ID, FOLLOW_ID_in_member550);
            if (state.failed) return type;
            if (state.backtracking == 1) {

              Symbol s = currentScope.resolve((ID8 != null ? ID8.getText() : null));
              System.out.println(
                  "line "
                      + ID8.getLine()
                      + ": ref "
                      + (ID8 != null ? ID8.getText() : null)
                      + "="
                      + s);
              if (s != null) type = s.type;
            }
          }
          break;
      }
    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return type;
  }
示例#5
0
文件: DefRef.java 项目: chuzui/antlr
  // $ANTLR start "varDeclaration"
  // DefRef.g:86:1: varDeclaration : ^( ( FIELD_DECL | VAR_DECL | ARG_DECL ) type ID ( . )? ) ;
  public final void varDeclaration() throws RecognitionException {
    CommonTree ID4 = null;
    DefRef.type_return type5 = null;

    try {
      // DefRef.g:87:5: ( ^( ( FIELD_DECL | VAR_DECL | ARG_DECL ) type ID ( . )? ) )
      // DefRef.g:87:9: ^( ( FIELD_DECL | VAR_DECL | ARG_DECL ) type ID ( . )? )
      {
        if (input.LA(1) == ARG_DECL || (input.LA(1) >= VAR_DECL && input.LA(1) <= FIELD_DECL)) {
          input.consume();
          state.errorRecovery = false;
          state.failed = false;
        } else {
          if (state.backtracking > 0) {
            state.failed = true;
            return;
          }
          MismatchedSetException mse = new MismatchedSetException(null, input);
          throw mse;
        }

        match(input, Token.DOWN, null);
        if (state.failed) return;
        pushFollow(FOLLOW_type_in_varDeclaration363);
        type5 = type();

        state._fsp--;
        if (state.failed) return;
        ID4 = (CommonTree) match(input, ID, FOLLOW_ID_in_varDeclaration365);
        if (state.failed) return;
        // DefRef.g:87:50: ( . )?
        int alt5 = 2;
        int LA5_0 = input.LA(1);

        if (((LA5_0 >= METHOD_DECL && LA5_0 <= 30))) {
          alt5 = 1;
        }
        switch (alt5) {
          case 1:
            // DefRef.g:87:50: .
            {
              matchAny(input);
              if (state.failed) return;
            }
            break;
        }

        match(input, Token.UP, null);
        if (state.failed) return;
        if (state.backtracking == 1) {

          System.out.println(
              "line " + ID4.getLine() + ": def " + (ID4 != null ? ID4.getText() : null));
          VariableSymbol vs =
              new VariableSymbol(
                  (ID4 != null ? ID4.getText() : null), (type5 != null ? type5.tsym : null));
          currentScope.define(vs);
        }
      }

    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return;
  }
示例#6
0
文件: DefRef.java 项目: chuzui/antlr
  // $ANTLR start "enterMethod"
  // DefRef.g:66:1: enterMethod : ^( METHOD_DECL type ID ( . )* ) ;
  public final void enterMethod() throws RecognitionException {
    CommonTree ID2 = null;
    DefRef.type_return type3 = null;

    try {
      // DefRef.g:67:5: ( ^( METHOD_DECL type ID ( . )* ) )
      // DefRef.g:67:9: ^( METHOD_DECL type ID ( . )* )
      {
        match(input, METHOD_DECL, FOLLOW_METHOD_DECL_in_enterMethod284);
        if (state.failed) return;

        match(input, Token.DOWN, null);
        if (state.failed) return;
        pushFollow(FOLLOW_type_in_enterMethod286);
        type3 = type();

        state._fsp--;
        if (state.failed) return;
        ID2 = (CommonTree) match(input, ID, FOLLOW_ID_in_enterMethod288);
        if (state.failed) return;
        // DefRef.g:67:31: ( . )*
        loop4:
        do {
          int alt4 = 2;
          int LA4_0 = input.LA(1);

          if (((LA4_0 >= METHOD_DECL && LA4_0 <= 30))) {
            alt4 = 1;
          } else if ((LA4_0 == UP)) {
            alt4 = 2;
          }

          switch (alt4) {
            case 1:
              // DefRef.g:67:31: .
              {
                matchAny(input);
                if (state.failed) return;
              }
              break;

            default:
              break loop4;
          }
        } while (true);

        match(input, Token.UP, null);
        if (state.failed) return;
        if (state.backtracking == 1) {

          System.out.println(
              "line " + ID2.getLine() + ": def method " + (ID2 != null ? ID2.getText() : null));
          MethodSymbol ms =
              new MethodSymbol(
                  (ID2 != null ? ID2.getText() : null),
                  (type3 != null ? type3.tsym : null),
                  currentScope);
          currentScope.define(ms); // def method in globals
          currentScope = ms; // set current scope to method scope
        }
      }

    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return;
  }
示例#7
0
文件: DefRef.java 项目: chuzui/antlr
  // $ANTLR start "enterStruct"
  // DefRef.g:48:1: enterStruct : ^( 'struct' ID ( . )+ ) ;
  public final void enterStruct() throws RecognitionException {
    CommonTree ID1 = null;

    try {
      // DefRef.g:49:5: ( ^( 'struct' ID ( . )+ ) )
      // DefRef.g:49:7: ^( 'struct' ID ( . )+ )
      {
        match(input, 17, FOLLOW_17_in_enterStruct219);
        if (state.failed) return;

        match(input, Token.DOWN, null);
        if (state.failed) return;
        ID1 = (CommonTree) match(input, ID, FOLLOW_ID_in_enterStruct221);
        if (state.failed) return;
        // DefRef.g:49:21: ( . )+
        int cnt3 = 0;
        loop3:
        do {
          int alt3 = 2;
          int LA3_0 = input.LA(1);

          if (((LA3_0 >= METHOD_DECL && LA3_0 <= 30))) {
            alt3 = 1;
          } else if ((LA3_0 == UP)) {
            alt3 = 2;
          }

          switch (alt3) {
            case 1:
              // DefRef.g:49:21: .
              {
                matchAny(input);
                if (state.failed) return;
              }
              break;

            default:
              if (cnt3 >= 1) break loop3;
              if (state.backtracking > 0) {
                state.failed = true;
                return;
              }
              EarlyExitException eee = new EarlyExitException(3, input);
              throw eee;
          }
          cnt3++;
        } while (true);

        match(input, Token.UP, null);
        if (state.failed) return;
        if (state.backtracking == 1) {

          System.out.println(
              "line " + ID1.getLine() + ": def struct " + (ID1 != null ? ID1.getText() : null));
          StructSymbol ss = new StructSymbol((ID1 != null ? ID1.getText() : null), currentScope);
          currentScope.define(ss); // def struct in current scope
          currentScope = ss; // set current scope to struct scope
        }
      }

    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return;
  }