コード例 #1
0
  @Override
  public Node parse(Token token) throws IOException {
    AttrNode node = null;
    if (ExpressionParser.START.contains(token.type())) {
      node = new AttrNode(startLine());
      token = tokenizer().peek();
      Token var = tokenizer().current();
      if (ATTR.contains(token.type())) {
        boolean first = true;
        while (ATTR.contains(token.type())) {
          Node n = null;
          if (tokenizer().peek().type() == TokenType.LEFT_PAREN) {
            CallParser call = new CallParser(this);
            n = call.parse(var);
          } else if (ExpressionParser.START.contains(var.type())) {
            if (first) {
              n = ParseUtil.value(var);
              first = false;
            } else {
              n = ParseUtil.var(var);
            }
          } else {
            error(ErrorCode.INVALID_EXPR);
          }

          if (n != null) {
            node.add(n);
            token = tokenizer().next();
            if (ATTR.contains(token.type())) {
              var = tokenizer().next();
            }
          }
        }
      } else if (tokenizer().current().type() == TokenType.LEFT_BRACKET) {
        ArrayParser arrp = new ArrayParser(this);
        Node n = arrp.parse(tokenizer().current());
        node.add(n);
      } else {
        node.add(ParseUtil.value(tokenizer().current()));
        tokenizer().next();
      }

    } else {
      error(ErrorCode.INVALID_EXPR);
    }

    return node;
  }
コード例 #2
0
ファイル: ExpressionParser.java プロジェクト: KukaTails/tpsc
  private ICodeNode parseIdentifier(Token token) throws Exception {
    ICodeNode rootNode = null;

    // Look up the identifier in the symbol table stack.
    // Flag the identifier as undefined if it's not found.
    String name = token.getText().toLowerCase();
    SymTabEntry id = symTabStack.lookup(name);

    // Undefined.
    if (id == null) {
      errorHandler.flag(token, IDENTIFIER_UNDEFINED, this);
      id = symTabStack.enterLocal(name);
      id.setDefinition(UNDEFINED);
      id.setTypeSpec(Predefined.undefinedType);
    }

    Definition defnCode = id.getDefinition();
    switch ((DefinitionImpl) defnCode) {
      case CONSTANT:
        {
          Object value = id.getAttribute(CONSTANT_VALUE);
          TypeSpec type = id.getTypeSpec();

          if (value instanceof Integer) {
            rootNode = ICodeFactory.createICodeNode(INTEGER_CONSTANT);
            rootNode.setAttribute(VALUE, value);
          } else if (value instanceof Float) {
            rootNode = ICodeFactory.createICodeNode(REAL_CONSTANT);
            rootNode.setAttribute(VALUE, value);
          } else if (value instanceof String) {
            rootNode = ICodeFactory.createICodeNode(STRING_CONSTANT);
            rootNode.setAttribute(VALUE, value);
          }

          id.appendLineNumber(token.getLineNumber());
          token = nextToken(); // consume the constant identifier

          if (rootNode != null) {
            rootNode.setTypeSpec(type);
          }

          break;
        }

      case ENUMERATION_CONSTANT:
        {
          Object value = id.getAttribute(CONSTANT_VALUE);
          TypeSpec type = id.getTypeSpec();

          rootNode = ICodeFactory.createICodeNode(INTEGER_CONSTANT);
          rootNode.setAttribute(VALUE, value);

          id.appendLineNumber(token.getLineNumber());
          token = nextToken(); // consume the enum constant identifier

          rootNode.setTypeSpec(type);
          break;
        }

      case FUNCTION:
        {
          CallParser callParser = new CallParser(this);
          rootNode = callParser.parse(token);
          break;
        }
      default:
        {
          VariableParser variableParser = new VariableParser(this);
          rootNode = variableParser.parse(token, id);
          break;
        }
    }
    return rootNode;
  }