All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javacc-7.0.1.examples.JJTreeExamples.cpp.eg4.Parser.cc Maven / Gradle / Ivy

There is a newer version: 7.0.13
Show newest version
/* Parser.cc */
#include "Parser.h"
#include "TokenMgrError.h"
#include "SimpleNode.h"
namespace EG4 {
  unsigned int jj_la1_0[] = {
0x18000,0x18000,0xe0000,0xe0000,0x100880,};

  /** Constructor with user supplied TokenManager. */



ASTStart        * Parser::Start() {/*@bgen(jjtree) Start */
  ASTStart *jjtn000 = new ASTStart(JJTSTART);
  bool jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
    try {
      Expression();
      jj_consume_token(14);
jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
return jjtn000;
    } catch ( ...) {
if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    }
if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
assert(false);
}


void Parser::Expression() {
    AdditiveExpression();
}


void Parser::AdditiveExpression() {
ASTAdd *jjtn001 = new ASTAdd(JJTADD);
    bool jjtc001 = true;
    jjtree.openNodeScope(jjtn001);
    try {
      MultiplicativeExpression();
      while (!hasError) {
        switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
        case 15:
        case 16:{
          ;
          break;
          }
        default:
          jj_la1[0] = jj_gen;
          goto end_label_1;
        }
        switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
        case 15:{
          jj_consume_token(15);
          break;
          }
        case 16:{
          jj_consume_token(16);
          break;
          }
        default:
          jj_la1[1] = jj_gen;
          jj_consume_token(-1);
          errorHandler->handleParseError(token, getToken(1), __FUNCTION__, this), hasError = true;
        }
        MultiplicativeExpression();
      }
      end_label_1: ;
    } catch ( ...) {
if (jjtc001) {
      jjtree.clearNodeScope(jjtn001);
      jjtc001 = false;
    } else {
      jjtree.popNode();
    }
    }
if (jjtc001) {
      jjtree.closeNodeScope(jjtn001, jjtree.nodeArity() > 1);
    }
}


void Parser::MultiplicativeExpression() {
ASTMult *jjtn001 = new ASTMult(JJTMULT);
    bool jjtc001 = true;
    jjtree.openNodeScope(jjtn001);
    try {
      UnaryExpression();
      while (!hasError) {
        switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
        case 17:
        case 18:
        case 19:{
          ;
          break;
          }
        default:
          jj_la1[2] = jj_gen;
          goto end_label_2;
        }
        switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
        case 17:{
          jj_consume_token(17);
          break;
          }
        case 18:{
          jj_consume_token(18);
          break;
          }
        case 19:{
          jj_consume_token(19);
          break;
          }
        default:
          jj_la1[3] = jj_gen;
          jj_consume_token(-1);
          errorHandler->handleParseError(token, getToken(1), __FUNCTION__, this), hasError = true;
        }
        UnaryExpression();
      }
      end_label_2: ;
    } catch ( ...) {
if (jjtc001) {
      jjtree.clearNodeScope(jjtn001);
      jjtc001 = false;
    } else {
      jjtree.popNode();
    }
    }
if (jjtc001) {
      jjtree.closeNodeScope(jjtn001, jjtree.nodeArity() > 1);
    }
}


void Parser::UnaryExpression() {
    switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
    case 20:{
      jj_consume_token(20);
      Expression();
      jj_consume_token(21);
      break;
      }
    case IDENTIFIER:{
      Identifier();
      break;
      }
    case INTEGER_LITERAL:{
      Integer();
      break;
      }
    default:
      jj_la1[4] = jj_gen;
      jj_consume_token(-1);
      errorHandler->handleParseError(token, getToken(1), __FUNCTION__, this), hasError = true;
    }
}


void Parser::Identifier() {Token* t = NULL;/*@bgen(jjtree) MyOtherID */
  ASTMyOtherID *jjtn000 = new ASTMyOtherID(JJTMYOTHERID);
  bool jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
    try {
      t = jj_consume_token(IDENTIFIER);
jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
jjtn000->setName(t->image);
    } catch ( ...) {
if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    }
if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
}


void Parser::Integer() {/*@bgen(jjtree) Integer */
  ASTInteger *jjtn000 = new ASTInteger(JJTINTEGER);
  bool jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
    try {
      jj_consume_token(INTEGER_LITERAL);
    } catch ( ...) {
if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    }
if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
}


  Parser::Parser(TokenManager *tokenManager){
    head = nullptr;
    ReInit(tokenManager);
}
Parser::~Parser()
{
  clear();
}

void Parser::ReInit(TokenManager* tokenManager){
    clear();
    errorHandler = new ErrorHandler();
    hasError = false;
    token_source = tokenManager;
    head = token = new Token();
    token->kind = 0;
    token->next = nullptr;
    jj_lookingAhead = false;
    jj_rescan = false;
    jj_done = false;
    jj_scanpos = jj_lastpos = nullptr;
    jj_gc = 0;
    jj_kind = -1;
    indent = 0;
    trace = false;
    jj_ntk = -1;
    jjtree.reset();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }


void Parser::clear(){
  //Since token manager was generate from outside,
  //parser should not take care of deleting
  //if (token_source) delete token_source;
  if (head) {
    Token *next, *t = head;
    while (t) {
      next = t->next;
      delete t;
      t = next;
    }
  }
  if (errorHandler) {
    delete errorHandler, errorHandler = nullptr;
  }
}


Token * Parser::jj_consume_token(int kind)  {
    Token *oldToken;
    if ((oldToken = token)->next != nullptr) token = token->next;
    else token = token->next = token_source->getNextToken();
    jj_ntk = -1;
    if (token->kind == kind) {
      jj_gen++;
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    JJString image = kind >= 0 ? tokenImage[kind] : tokenImage[0];
    errorHandler->handleUnexpectedToken(kind, image.substr(1, image.size() - 2), getToken(1), this);
    hasError = true;
    return token;
  }


/** Get the next Token. */

Token * Parser::getNextToken(){
    if (token->next != nullptr) token = token->next;
    else token = token->next = token_source->getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

/** Get the specific Token. */

Token * Parser::getToken(int index){
    Token *t = token;
    for (int i = 0; i < index; i++) {
      if (t->next != nullptr) t = t->next;
      else t = t->next = token_source->getNextToken();
    }
    return t;
  }


int Parser::jj_ntk_f(){
    if ((jj_nt=token->next) == nullptr)
      return (jj_ntk = (token->next=token_source->getNextToken())->kind);
    else
      return (jj_ntk = jj_nt->kind);
  }


 void  Parser::parseError()   {
      fprintf(stderr, "Parse error at: %d:%d, after token: %s encountered: %s\n", token->beginLine, token->beginColumn, addUnicodeEscapes(token->image).c_str(), addUnicodeEscapes(getToken(1)->image).c_str());
   }


  bool Parser::trace_enabled()  {
    return trace;
  }


  void Parser::enable_tracing()  {
  }

  void Parser::disable_tracing()  {
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy