コード例 #1
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
  private NaleNode statement0() throws JmoException {

    Token t = buf.findValid();
    if (t == null) {
      return null;
    }
    if (t.getType() == TokenType.IF) {
      buf.takeToken();
      Token tx = buf.findValid();
      this.expectNotNull(tx);
      NaleNode nq;
      if (tx.getType() == TokenType.TRUE) {
        nq = new BooleanNode(true, tx.getPosition());
        buf.takeToken();
      } else if (tx.getType() == TokenType.FALSE) {
        nq = new BooleanNode(false, tx.getPosition());
        buf.takeToken();
      } else {
        nq = parseVariable();
      }
      this.expectLineEnd();
      return new IFNode(nq, t.getPosition());
    } else if (t.getType() == TokenType.FI) {
      NaleNode node = new IFEndNode(t.getPosition());
      buf.takeToken();
      expectEnd();
      return node;
    } else if (t.getType() == TokenType.DEBUG) {
      buf.takeToken();
      expectEnd();
      return new DebuggerNode(t.getPosition());

    } else if (t.getType() == TokenType.EXPORT) {
      buf.takeToken();
      NaleNode nn = statement();
      if (nn instanceof VarNode) {

        VarNode vn = (VarNode) nn;
        if (vn.getParNode() != null) {
          throw new ParseException("can not export varNode with member:" + vn, t.getPosition());
        }
        return new ExpNode(t.getPosition(), (VarNode) nn);
      } else if (nn instanceof AssignableNode) {
        return new ExpNode(t.getPosition(), (AssignableNode) nn);
      } else {
        throw new ParseException("can not export non-varNode! " + nn, t.getPosition());
      }
    } else if (t.getType() == TokenType.echo) {
      buf.takeToken();
      Token n = buf.findValid();
      this.expectNotNull(n);
      NaleNode node = regexNode(t.getPosition());
      return new EchoNode(node, t.getPosition());
    } else {
      return statement();
    }
  }
コード例 #2
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
 protected VarNode varNode() throws JmoException {
   Token t = buf.findValid();
   checkType(t, null);
   String name = t.getText();
   if (validMethodName(name)) {
     buf.takeToken();
     return new VarNode(t.getPosition(), name);
   }
   throw new ParseException("invalid varNode " + t, t.getPosition());
 }
コード例 #3
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
  private NaleNode statement() throws JmoException {

    Token t = buf.findValid();
    checkType(t, null);
    if (t.getType() == TokenType.SHARP) {
      buf.takeToken();
      t = buf.findValid();
      checkType(t, TokenType.VARIABLE);

      NaleNode nn = parseVariable();
      if (nn instanceof VarNode) {
        buf.findValid();
        expect(TokenType.ASSIGN);

        t = buf.findValid();
        checkType(t, null);
        return this.assign((VarNode) nn, true);

      } else {
        throw new ParseException("expect $ or exp but get " + nn, t.getPosition());
      }

    } else {
      if (t.getType() == TokenType.DOLLAR) {
        buf.takeToken();
      }
      t = buf.findValid();
      checkType(t, null);
      if (t.getType() == TokenType.VARIABLE) {
        NaleNode nn = parseVariable();
        if (nn instanceof VarNode) {
          Token next = buf.findValid();
          if (next == null) {
            return new FunctionCallNode(
                t.getPosition(), (VarNode) nn, "print", new ArrayList<ReferNode>(0));
          } else {
            expect(TokenType.ASSIGN);

            t = buf.findValid();
            checkType(t, null);
            return this.assign((VarNode) nn, false);
          }
        } else if (nn instanceof FunctionCallNode) {
          return nn;
        } else {
          throw new ParseException("expect $ or exp but get " + nn, t.getPosition());
        }
      } else {
        throw new ParseException("expect variable, but get " + t, t.getPosition());
      }
    }
  }
コード例 #4
0
ファイル: LiteralParser.java プロジェクト: rwaldron/magpie
  @Override
  public Expr parse(MagpieParser parser, Token token) {
    switch (token.getType()) {
      case BOOL:
        return Expr.bool(token.getPosition(), token.getBool());
      case INT:
        return Expr.int_(token.getPosition(), token.getInt());
      case NOTHING:
        return Expr.nothing(token.getPosition());
      case STRING:
        return Expr.string(token.getPosition(), token.getString());
    }

    throw new ParseException("Unexpected token type for literal.");
  }
コード例 #5
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
  private Command parseCommand() throws CompileException {

    Command returnCommand;

    switch (currentToken.getTokenKind()) {
      case MOVE:
        returnCommand = new Command(parseCommandMove());
        break;
      case GOTO:
        returnCommand = new Command(parseCommandGoto());
        break;
      case SET:
        returnCommand = new Command(parseVarAssignmentCommand());
        break;
      case IDENTIFIER:
        Identifier identifierArea = parseIdentifier();
        accept(TokenKind.DOT);
        returnCommand = new Command(identifierArea, parseIdentifier(), parseParameters());
        break;
      case IF:
        return new Command(parseIfStatement()); // If doesn't need ';' so it's returned turn here.
      default:
        throw new CompileException(
            ErrorType.UNEXPECTED_TOKEN,
            currentToken.getPosition(),
            "Wrong token in parseCommand, got " + currentToken.getTokenKind());
    }
    accept(TokenKind.SEMICOLON);
    return returnCommand;
  }
コード例 #6
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
  protected NaleNode parseVariable() throws JmoException {
    VarNode varNode = varNode();
    while (true) {
      Token t = buf.findValid();
      if (t == null) {
        return varNode;
      }
      if (t.getType() == TokenType.LPAREN) { // function
        expectValid(TokenType.LPAREN);
        List<ReferNode> paras = funcParasNode();
        expectValid(TokenType.RPAREN);
        FunctionCallNode fncN =
            new FunctionCallNode(
                t.getPosition(), varNode.getParNode(), varNode.getVarName(), paras);
        expectLineEnd();
        return fncN;
      } else if (t.getType() == TokenType.DOT) {
        buf.takeToken();
        VarNode varsub = varNode();
        varNode = new VarNode(varNode.getPosition(), varNode, varsub.getVarName());
      } else {

        return varNode;
      }
    }
  }
コード例 #7
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
  private void expectEnd() {
    Token tx = buf.findValid();

    if (tx != null) {
      throw new JmoException(
          "expect EOF, but not expected token after debug " + tx, tx.getPosition());
    }
  }
コード例 #8
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
 /*
  *	Generel parsing methods:
  */
 private Token accept(TokenKind expectedKind) throws CompileException {
   if (currentToken.getTokenKind() != expectedKind)
     throw new CompileException(
         ErrorType.UNEXPECTED_TOKEN,
         currentToken.getPosition(),
         "Wrong token, got " + currentToken.getTokenKind() + " ,expected " + expectedKind);
   Token previousToken = currentToken;
   acceptIt();
   return previousToken;
 }
コード例 #9
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
  private Factor parseFactor() throws CompileException {
    boolean isNegative = false;

    while (true) {
      switch (currentToken.getTokenKind()) {
        case TERMOPERATOR:
          if (currentToken.getSpelling().equals("-")) {
            acceptIt();
            isNegative = true;
            break;
          } else
            throw new CompileException(
                ErrorType.UNEXPECTED_TOKEN,
                currentToken.getPosition(),
                "Unexpected token: " + currentToken.getSpelling());

        case LPAREN:
          acceptIt();
          ArithmeticExpression arithmeticExpression = parseArithmeticExpression();
          accept(TokenKind.RPAREN);
          return new Factor(arithmeticExpression, isNegative);

        case INTLITERAL:
          return new Factor(parseNumber(), isNegative);

        case IDENTIFIER:
          Identifier identifierArea = parseIdentifier();
          if (currentToken.getTokenKind() == TokenKind.DOT) {
            acceptIt();
            return new Factor(identifierArea, parseIdentifier(), parseParameters(), isNegative);
          } else {
            return new Factor(identifierArea, isNegative);
          }

        default:
          throw new CompileException(
              ErrorType.UNEXPECTED_TOKEN,
              currentToken.getPosition(),
              "Wrong token, in parseFactor: " + currentToken.getTokenKind());
      }
    }
  }
コード例 #10
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
  private ExpressionBlock parseExpressionBlock() throws CompileException {
    boolean isNegative = false;

    while (true) {
      switch (currentToken.getTokenKind()) {
        case TERMOPERATOR:
          if (currentToken.getSpelling().equals("-")) {
            acceptIt();
            isNegative = true;
            break;
          } else
            throw new CompileException(
                ErrorType.UNEXPECTED_TOKEN,
                currentToken.getPosition(),
                "Unexpected token: " + currentToken.getSpelling());

        case IDENTIFIER:
          Identifier identifier = parseIdentifier();
          if (currentToken.getTokenKind() == TokenKind.DOT) {
            acceptIt();
            return new ExpressionBlock(
                identifier, parseIdentifier(), parseParameters(), isNegative);
          } else return new ExpressionBlock(identifier, isNegative);
        case STRINGLITERAL:
        case INTLITERAL:
        case BOOLLITERAL:
          return new ExpressionBlock(parseLiteral(), isNegative);
        case LPAREN:
          acceptIt();
          Expression expression = parseExpression();
          accept(TokenKind.RPAREN);

          return new ExpressionBlock(expression, isNegative);
        default:
          throw new CompileException(
              ErrorType.UNEXPECTED_TOKEN,
              currentToken.getPosition(),
              "Wrong token, in parseExpressionBlock: " + currentToken.getTokenKind());
      }
    }
  }
コード例 #11
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
 private void expect(TokenType type) {
   Token t = buf.takeToken();
   if (t == null) {
     throw new ParseException(
         "expect " + type + ", but get token " + null + ", at line " + this.lineNo);
   } else {
     if (type == t.getType()) {
       return;
     } else {
       throw new ParseException("expect " + type + ", but get token " + t, t.getPosition());
     }
   }
 }
コード例 #12
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
  private CommandGoto parseCommandGoto() throws CompileException {
    accept(TokenKind.GOTO);

    switch (currentToken.getTokenKind()) {
      case IDENTIFIER:
        return new CommandGoto(parseIdentifier());
      case LSQBRACKET:
        return new CommandGoto(parseCoordinate());
      default:
        throw new CompileException(
            ErrorType.UNEXPECTED_TOKEN,
            currentToken.getPosition(),
            "Wrong token, in parseMoveCommand, Got " + currentToken.getTokenKind() + ".");
    }
  }
コード例 #13
0
ファイル: Sintatico.java プロジェクト: hpzona/Compiladores
  private boolean step() throws LexicalError, SyntaticError, SemanticError {
    if (currentToken == null) {
      int pos = 0;
      if (previousToken != null)
        pos = previousToken.getPosition() + previousToken.getLexeme().length();

      currentToken = new Token(DOLLAR, "$", pos);
    }

    int x = ((Integer) stack.pop()).intValue();
    int a = currentToken.getId();

    if (x == EPSILON) {
      return false;
    } else if (isTerminal(x)) {
      if (x == a) {
        if (stack.empty()) return true;
        else {
          previousToken = currentToken;
          currentToken = scanner.nextToken();
          return false;
        }
      } else {
        throw new SyntaticError(PARSER_ERROR[x], currentToken.getPosition());
      }
    } else if (isNonTerminal(x)) {
      if (pushProduction(x, a)) return false;
      else throw new SyntaticError(PARSER_ERROR[x], currentToken.getPosition());
    } else // isSemanticAction(x)
    {
      if (executaAcoesSemanticas) {
        semanticAnalyser.executeAction(x - FIRST_SEMANTIC_ACTION, previousToken);
      }
      return false;
    }
  }
コード例 #14
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
  private BooleanExpression parseBooleanExpression() throws CompileException {
    if (currentToken.getTokenKind() == TokenKind.ISBOOLEXP) {
      accept(TokenKind.ISBOOLEXP);
      return new BooleanExpression(
          parseExpressionBlock(), parseLogicalOperator(), parseExpressionBlock());
    } else {
      if (currentToken.getTokenKind() != TokenKind.BOOLLITERAL)
        throw new CompileException(
            ErrorType.UNEXPECTED_TOKEN,
            currentToken.getPosition(),
            "Wrong token. Got: "
                + currentToken.getTokenKind().toString()
                + ", expected: BOOLLITERAL.");

      return new BooleanExpression(parseExpressionBlock());
    }
  }
コード例 #15
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
 private Literal parseLiteral() throws CompileException {
   switch (currentToken.getTokenKind()) {
     case TERMOPERATOR:
       if (currentToken.getSpelling().equals("-")) {
         acceptIt();
         return new Literal(parseNumber());
       }
     case INTLITERAL:
       return new Literal(parseNumber());
     case STRINGLITERAL:
       return new Literal(parseString());
     case BOOLLITERAL:
       return new Literal(parseBoolean());
     default:
       throw new CompileException(
           ErrorType.UNEXPECTED_TOKEN,
           currentToken.getPosition(),
           "Wrong token, in parseLiteral, got " + currentToken.getTokenKind());
   }
 }
コード例 #16
0
ファイル: Utterance.java プロジェクト: helang818/PolyU_MScST
  /**
   * Sets the token list for this utterance. Note that this could be optimized by turning the token
   * list directly into the token relation.
   *
   * @param tokenList the tokenList
   */
  private void setTokenList(List tokenList) {
    setInputText(tokenList);

    Relation relation = createRelation(Relation.TOKEN);
    for (Iterator i = tokenList.iterator(); i.hasNext(); ) {
      Token token = (Token) i.next();
      String tokenWord = token.getWord();

      if (tokenWord != null && tokenWord.length() > 0) {
        Item item = relation.appendItem();

        FeatureSet featureSet = item.getFeatures();
        featureSet.setString("name", tokenWord);
        featureSet.setString("whitespace", token.getWhitespace());
        featureSet.setString("prepunctuation", token.getPrepunctuation());
        featureSet.setString("punc", token.getPostpunctuation());
        featureSet.setString("file_pos", String.valueOf(token.getPosition()));
        featureSet.setString("line_number", String.valueOf(token.getLineNumber()));
      }
    }
  }
コード例 #17
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
  private NaleNode assign(VarNode varNode, boolean isSharp) throws JmoException {

    Token next = buf.findValid();
    checkType(next, null);
    if (isSharp) {
      if (next.getType() == TokenType.AT) {
        buf.takeToken();
        String nextStr = collectToEnd();
        return new AssignAtNode(buf.currentPosition(), varNode, nextStr);
      } else {
        RegexExpressionNode expression = regexNode(next.getPosition());
        return new AssignNode(buf.currentPosition(), varNode, expression);
      }
    } else {
      NaleNode node = expression(varNode);
      if (node instanceof ValueNode) {
        return new AssignNode(buf.currentPosition(), varNode, (ValueNode) node);
      } else {
        throw new RuntimeException("not value node " + buf.currentPosition());
      }
    }
  }
コード例 #18
0
ファイル: Parser.java プロジェクト: julemand101/KittyCraneium
  private EventCommand parseEventCommand() throws CompileException {
    ASTList<VarDeclaration> varDeclarations = new ASTList<VarDeclaration>();
    ASTList<Command> commands = new ASTList<Command>();
    Coordinates inCoordinates = null;
    WhereExpression whereExpression = null;
    Identifier identifierArea = null;
    Identifier identifierMethod = null;
    Parameters parameters = null;

    accept(TokenKind.EVENT);
    Type type = parseType();
    Identifier identifierEvent = parseIdentifier();
    // Does the EVENT contain an IN clause?
    if (currentToken.getTokenKind() == TokenKind.IN) {
      acceptIt(); // accept the IN token

      // Is it IN COORDINATES or an IDENTIFIER?
      if (currentToken.getTokenKind() == TokenKind.LSQBRACKET) {
        // its IN COORDINATES
        inCoordinates = parseCoordinates();
      } else if (currentToken.getTokenKind() == TokenKind.IDENTIFIER) {
        // It's IN an IDENTIFIER or a METHODCALL
        identifierArea = parseIdentifier();
        if (currentToken.getTokenKind() == TokenKind.DOT) {
          // It's a METHODCALL
          acceptIt();
          identifierMethod = parseIdentifier();
          parameters = parseParameters();
        }
      }
    }
    // Does the EVENT contain a WHERE clause?
    if (currentToken.getTokenKind() == TokenKind.WHERE) {
      whereExpression = parseWhereExpression();
    }
    // Start parsing variableDeclarations.
    accept(TokenKind.LCRLPARAN);

    while (currentToken.getTokenKind() == TokenKind.TYPE)
      varDeclarations.add(parseVarDeclaration());

    // Start parsing commands.
    while (currentToken.getTokenKind() != TokenKind.RCRLPARAN) commands.add(parseCommand());

    accept(TokenKind.RCRLPARAN);

    // Find the correct constructor.
    if (inCoordinates != null) {
      if (whereExpression != null) {
        // IN COORDINATES WHERE
        return new EventCommand(
            type, identifierEvent, inCoordinates, whereExpression, varDeclarations, commands);
      }
      // IN COORDINATES
      return new EventCommand(type, identifierEvent, inCoordinates, varDeclarations, commands);
    } else if (identifierMethod != null) {
      if (whereExpression != null) {
        // IN METHODCALL WHERE
        return new EventCommand(
            type,
            identifierEvent,
            identifierArea,
            identifierMethod,
            parameters,
            whereExpression,
            varDeclarations,
            commands);
      }
      // IN METHODCALL
      return new EventCommand(
          type,
          identifierEvent,
          identifierArea,
          identifierMethod,
          parameters,
          varDeclarations,
          commands);
    } else if (identifierArea != null) {
      if (whereExpression != null) {
        // IN IDENTIFIER WHERE
        return new EventCommand(
            type, identifierEvent, identifierArea, whereExpression, varDeclarations, commands);
      }
      // IN IDENTIFIER
      return new EventCommand(type, identifierEvent, identifierArea, varDeclarations, commands);
    } else if (whereExpression != null) {
      return new EventCommand(type, identifierEvent, whereExpression, varDeclarations, commands);
    } else {
      // We need either an IN or WHERE or Both...
      throw new CompileException(
          ErrorType.UNEXPECTED_TOKEN,
          currentToken.getPosition(),
          "parseEvent syntax error, EVENT's needs either an IN or WHERE clause, or Both, got "
              + currentToken.getTokenKind());
    }
  }
コード例 #19
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
 private void expectLineEnd() throws JmoException {
   Token t = buf.getToken(0);
   if (t != null) {
     throw new ParseException("expect to lineEnd but get token " + t, t.getPosition());
   }
 }
コード例 #20
0
ファイル: LineParser0.java プロジェクト: hetykai/gyuhji56
  private Regex varPartial() throws JmoException {
    VarsPartial varRegex = new VarsPartial(buf.currentPosition());
    boolean hasNext = true;
    while (true) {
      Token next = buf.getToken(0);
      if (next == null) {
        throw new RuntimeException("expect } at line end");
      }
      if (next.getType() == TokenType.RBRACE) {
        break;
      } else if (next.getType() == TokenType.OR) {
        buf.takeToken();
        continue;
      } else if (next.getType() == TokenType.WHITE) {
        buf.takeToken();
        continue;
      } else {
        if (next.getType() == TokenType.DOLLAR) {
          buf.takeToken();
          next = buf.findValid();
        }

        VarNode vn = this.varNode();
        Token expNode = buf.findValid();
        if (expNode == null) {
          throw new ParseException("'}' NOT found", next.getPosition());
        }
        // var.0 不允许是
        // var.0- may
        // var.1- no less than
        // var.2-4 [2,4]个
        int min = -1, max = -1;
        if (expNode.getType() == TokenType.DOT) {
          buf.takeToken();
          Token expNum = buf.findValid();

          try {
            min = Integer.parseInt(expNum.getText());
          } catch (Exception e) {
            throw new ParseException(
                "not a valid number min repeat number for " + vn + ", token is '" + expNum + "'",
                expNum.getPosition());
          }
          buf.takeToken();

          Token expMinus = buf.findValid();
          if (expMinus.getType() == TokenType.MINUS) {
            buf.takeToken();
            Token expMax = buf.findValid();
            if (expMax.getType() == TokenType.RBRACE || expMax.getType() == TokenType.OR) {
              max = Integer.MAX_VALUE;
            } else {
              try {
                max = Integer.parseInt(expMax.getText());
              } catch (Exception e) {
                throw new ParseException(
                    "not a valid number max repeat number for "
                        + vn
                        + ", token is '"
                        + expNum
                        + "'",
                    expMax.getPosition());
              }
              buf.takeToken();
            }
          }
        }

        varRegex.addVar(expNode.getPosition(), vn, min, max);
        hasNext = false;
      }
    }
    if (hasNext) {
      throw new ParseException("expect regex expression, but not found", buf.currentPosition());
    }
    return varRegex;
  }