IntegerLiteralValueDef::IntegerLiteralValueDef(const pANTLR3_BASE_TREE node) : LiteralValueDef(INTEGER_LITERAL, node) { assert(node->getType(node) == N_INT_LITERAL); assert(node->getChildCount(node) == 1); pANTLR3_BASE_TREE n = (pANTLR3_BASE_TREE) node->getChild(node, 0); assert(n->getType(n) == INTL); wchar_t* szStr = (wchar_t*) n->getText(n)->chars; int value; try { value = boost::lexical_cast<int, wchar_t*>(szStr); } catch (const std::exception&) { boost::wformat f(L"Invalid integer value: %1% at line %2%"); f % szStr % node->getLine(node); ParserException e(f.str()); throw e; } m_IntValue = value; }
void SCsTranslator::processSentenceAssign(pANTLR3_BASE_TREE node) { unsigned int nodesCount = node->getChildCount(node); assert(nodesCount == 2); pANTLR3_BASE_TREE node_left = (pANTLR3_BASE_TREE)node->getChild(node, 0); pANTLR3_BASE_TREE node_right = (pANTLR3_BASE_TREE)node->getChild(node, 1); pANTLR3_COMMON_TOKEN tok_left = node_left->getToken(node_left); pANTLR3_COMMON_TOKEN tok_right = node_left->getToken(node_right); assert(tok_left && tok_right); if (tok_left->type != ID_SYSTEM) { THROW_EXCEPT(Exception::ERR_PARSE, "Unsupported type of tokens at the left side of assignment sentence", mParams.fileName, tok_left->getLine(tok_left)); } if (tok_right->type == ID_SYSTEM) { mAssignments[GET_NODE_TEXT(node_left)] = GET_NODE_TEXT(node_right); } else { String left_idtf = (GET_NODE_TEXT(node_left)); sElement *el = parseElementTree(node_right, &left_idtf); } }
void SCsTranslator::dumpNode(pANTLR3_BASE_TREE node, std::ofstream &stream) { StringStream ss; ss << node; String s_root = ss.str().substr(3); pANTLR3_COMMON_TOKEN tok = node->getToken(node); if (tok) { stream << s_root << " [shape=box];" << std::endl; String label((const char*) node->getText(node)->chars); std::replace(label.begin(), label.end(), '"', '\''); stream << s_root << " [label=\"" << label << "\"];" << std::endl; } else stream << s_root << " [shape=circle];" << std::endl; uint32 n = node->getChildCount(node); for (uint32 i = 0; i < n; ++i) { pANTLR3_BASE_TREE child = (pANTLR3_BASE_TREE) node->getChild(node, i); StringStream s1; s1 << child; stream << s_root << " -> " << s1.str().substr(3) << ";" << std::endl; dumpNode(child, stream); } }
void SCsTranslator::processSentenceLevel1(pANTLR3_BASE_TREE node) { unsigned int nodesCount = node->getChildCount(node); assert(nodesCount == 3); pANTLR3_BASE_TREE node_obj = (pANTLR3_BASE_TREE)node->getChild(node, 0); pANTLR3_BASE_TREE node_pred = (pANTLR3_BASE_TREE)node->getChild(node, 1); pANTLR3_BASE_TREE node_subj = (pANTLR3_BASE_TREE)node->getChild(node, 2); pANTLR3_COMMON_TOKEN tok_pred = node_pred->getToken(node_pred); if (tok_pred->type != ID_SYSTEM) { THROW_EXCEPT(Exception::ERR_PARSE, String("Invalid predicate '") + ((const char*) node_pred->getText(node_pred)->chars) + "' in simple sentence", mParams.fileName, tok_pred->getLine(tok_pred)); } sElement *el_obj = parseElementTree(node_obj); sElement *el_subj = parseElementTree(node_subj); // determine arc type sc_type type = sc_type_edge_common; String pred = GET_NODE_TEXT(node_pred); size_t n = pred.find_first_of("#"); if (n != pred.npos) type = _getArcPreffixType(pred.substr(0, n)); _addEdge(el_obj, el_subj, type, false, pred); }
pANTLR3_BASE_TREE dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent) { pANTLR3_BASE_TREE newTree; pANTLR3_BASE_TREE child; pANTLR3_BASE_TREE newSubTree; ANTLR3_UINT32 n; ANTLR3_UINT32 i; if (t == NULL) { return NULL; } newTree = (pANTLR3_BASE_TREE)t->dupNode(t); // Ensure new subtree root has parent/child index set // adaptor->setChildIndex (adaptor, newTree, t->getChildIndex(t)); adaptor->setParent (adaptor, newTree, parent); n = adaptor->getChildCount (adaptor, t); for (i=0; i < n; i++) { child = (pANTLR3_BASE_TREE)adaptor->getChild (adaptor, t, i); newSubTree = (pANTLR3_BASE_TREE)adaptor->dupTreeTT (adaptor, child, t); adaptor->addChild (adaptor, newTree, newSubTree); } return newTree; }
ActualParamDef::ActualParamDef(const pANTLR3_BASE_TREE node) : ASTNode(node) { assert(node->getType(node) == N_ACTUAL_PARAM); assert(node->getChildCount(node) == 2); pANTLR3_BASE_TREE n; { // param name n = (pANTLR3_BASE_TREE) node->getChild(node, 0); assert(n->getType(n) == N_PARAM_NAME); assert(n->getChildCount(n) == 1); n = (pANTLR3_BASE_TREE) n->getChild(n, 0); m_ParamName = (wchar_t*) n->getText(n)->chars; } { // value n = (pANTLR3_BASE_TREE) node->getChild(node, 1); assert(n->getType(n) == N_VALUE); createValueDef(n, m_pValue); } }
ArrayInitValueDef::ArrayInitValueDef(const pANTLR3_BASE_TREE node) : ValueDef(ARRAY_INIT, node) { assert(node->getType(node) == N_ARRAY_INIT_VAL); assert(node->getChildCount(node) == 2); pANTLR3_BASE_TREE n, c; { // type n = (pANTLR3_BASE_TREE) node->getChild(node, 0); assert(n->getType(n) == N_ARRAY_TYPE); m_pDeclaredType = boost::shared_ptr<Type>(static_cast<Type*>(new ArrayType(n))); } { // array values n = (pANTLR3_BASE_TREE) node->getChild(node, 1); assert(n->getType(n) == N_ARRAY_VALUES); m_Values.clear(); int childCount = n->getChildCount(n); for (int i = 0; i < childCount; i++) { c = (pANTLR3_BASE_TREE) n->getChild(n, i); boost::shared_ptr<ValueDef> pValueDef; createValueDef(c, pValueDef); m_Values.push_back(pValueDef); } } }
/** Transform ^(nil x) to x */ static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root) { if (root != NULL && root->isNil(root) && root->getChildCount(root) == 1) { root = root->getChild(root, 0); } return root; }
pANTLR3_STRING emerson_printAST(pANTLR3_BASE_TREE tree, pANTLR3_UINT8* parserTokenNames) { pANTLR3_STRING string; ANTLR3_UINT32 i; ANTLR3_UINT32 n; pANTLR3_BASE_TREE t; if(tree->children == NULL || tree->children->size(tree->children) == 0) { return tree->toString(tree); } // THis is how you get a new string. The string is blank string = tree->strFactory->newRaw(tree->strFactory); if(tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8 (string, "("); pANTLR3_COMMON_TOKEN token = tree->getToken(tree); ANTLR3_UINT32 type = token->type; string->append(string, (const char*)parserTokenNames[type]); string->append8(string, " "); } if(tree->children != NULL) { n = tree->children->size(tree->children); for (i = 0; i < n; i++) { t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i); if (i > 0) { string->append8(string, " "); } string->appendS(string, emerson_printAST(t,parserTokenNames)); } } if(tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8(string,")"); } return string; }
/** If oldRoot is a nil root, just copy or move the children to newRoot. * If not a nil root, make oldRoot a child of newRoot. * * \code * old=^(nil a b c), new=r yields ^(r a b c) * old=^(a b c), new=r yields ^(r ^(a b c)) * \endcode * * If newRoot is a nil-rooted single child tree, use the single * child as the new root node. * * \code * old=^(nil a b c), new=^(nil r) yields ^(r a b c) * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) * \endcode * * If oldRoot was null, it's ok, just return newRoot (even if isNilNode). * * \code * old=null, new=r yields r * old=null, new=^(nil r) yields ^(nil r) * \endcode * * Return newRoot. Throw an exception if newRoot is not a * simple node or nil root with a single child node--it must be a root * node. If newRoot is <code>^(nil x)</endcode> return x as newRoot. * * Be advised that it's ok for newRoot to point at oldRoot's * children; i.e., you don't have to copy the list. We are * constructing these nodes so we should have this control for * efficiency. */ static pANTLR3_BASE_TREE becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree) { /* Protect against tree rewrites if we are in some sort of error * state, but have tried to recover. In C we can end up with a null pointer * for a tree that was not produced. */ if (newRootTree == NULL) { return oldRootTree; } /* root is just the new tree as is if there is no * current root tree. */ if (oldRootTree == NULL) { return newRootTree; } /* Produce ^(nil real-node) */ if (newRootTree->isNilNode(newRootTree)) { if (newRootTree->getChildCount(newRootTree) > 1) { /* TODO: Handle tree exceptions */ ANTLR3_FPRINTF(stderr, "More than one node as root! TODO: Create tree exception hndling\n"); return newRootTree; } /* The new root is the first child */ newRootTree = newRootTree->getChild(newRootTree, 0); } /* Add old root into new root. addChild takes care of the case where oldRoot * is a flat list (nill rooted tree). All children of oldroot are added to * new root. */ newRootTree->addChild(newRootTree, oldRootTree); /* Always returns new root structure */ return newRootTree; }
/** Add a child to the tree t. If child is a flat tree (a list), make all * in list children of t. Warning: if t has no children, but child does * and child isNilNode then it is ok to move children to t via * t.children = child.children; i.e., without copying the array. This * is for construction and I'm not sure it's completely general for * a tree's addChild method to work this way. Make sure you differentiate * between your tree's addChild and this parser tree construction addChild * if it's not ok to move children to t with a simple assignment. */ static void addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child) { if (t != NULL && child != NULL) { t->addChild(t, child); } }
static pANTLR3_STRING toStringTree (pANTLR3_BASE_TREE tree) { pANTLR3_STRING string; ANTLR3_UINT32 i; ANTLR3_UINT32 n; pANTLR3_BASE_TREE t; if (tree->children == NULL || tree->children->size(tree->children) == 0) { return tree->toString(tree); } /* Need a new string with nothing at all in it. */ string = tree->strFactory->newRaw(tree->strFactory); if (tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8 (string, "("); string->appendS (string, tree->toString(tree)); string->append8 (string, " "); } if (tree->children != NULL) { n = tree->children->size(tree->children); for (i = 0; i < n; i++) { t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i); if (i > 0) { string->append8(string, " "); } string->appendS(string, t->toStringTree(t)); } } if (tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8(string,")"); } return string; }
static void dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child) { if (t != NULL && child != NULL) { t->addChild(t, child); adaptor->debugger->addChild(adaptor->debugger, t, child); } }
static void replaceChildren (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t) { if (parent != NULL) { parent->replaceChildren(parent, startChildIndex, stopChildIndex, t); } }
static void setChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i, void * child) { if (tree->children == NULL) { tree->createChildrenList(tree); } tree->children->set(tree->children, i, child, NULL, ANTLR3_FALSE); }
static ANTLR3_UINT32 getCharPositionInLine (pANTLR3_BASE_TREE tree) { pANTLR3_COMMON_TOKEN token; token = ((pANTLR3_COMMON_TREE)(tree->super))->token; if (token == NULL || token->getCharPositionInLine(token) == -1) { if (tree->getChildCount(tree) > 0) { pANTLR3_BASE_TREE child; child = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); return child->getCharPositionInLine(child); } return 0; } return token->getCharPositionInLine(token); }
/// Set the parent and child indexes for some of the children of the /// supplied tree, starting with the child at the supplied index. /// static void freshenPACIndexes (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 offset) { ANTLR3_UINT32 count; ANTLR3_UINT32 c; count = tree->getChildCount(tree); // How many children do we have // Loop from the supplied index and set the indexes and parent // for (c = offset; c < count; c++) { pANTLR3_BASE_TREE child; child = tree->getChild(tree, c); child->setChildIndex(child, c); child->setParent(child, tree); } }
ASTNode::ASTNode(const pANTLR3_BASE_TREE node) { m_LineNumber = node->getLine(node); m_CharPosition = node->getCharPositionInLine(node); pANTLR3_BASE_TREE n = node; while ((n != NULL) && (n->u == NULL)) { if (n->u != NULL) { m_FileName = (wchar_t*) n->u; node->u = n->u; break; } else { n = n->getParent(n); } } }
/// Add all elements of the supplied list as children of this node /// static void addChildren (pANTLR3_BASE_TREE tree, pANTLR3_LIST kids) { ANTLR3_UINT32 i; ANTLR3_UINT32 s; s = kids->size(kids); for (i = 0; i<s; i++) { tree->addChild(tree, (pANTLR3_BASE_TREE)(kids->get(kids, i+1))); } }
void SCsTranslator::processSentenceLevel2_7(pANTLR3_BASE_TREE node) { String connector = GET_NODE_TEXT(node); // determine object pANTLR3_BASE_TREE node_obj = (pANTLR3_BASE_TREE)node->getChild(node, 0); sElement *el_obj = _createElement(GET_NODE_TEXT(node_obj), 0); // no we need to parse attributes and predicates processAttrsIdtfList(true, node, el_obj, connector, false); }
void emerson_createTreeMirrorImage2(pANTLR3_BASE_TREE ptr) { if(ptr!= NULL && ptr->children != NULL) { ANTLR3_UINT32 n = ptr->getChildCount(ptr); if(n == 1) { //emerson_createTreeMirrorImage((pANTLR3_BASE_TREE)(ptr->getChild(ptr, 0))); } if(n == 2) // should it be checked { pANTLR3_BASE_TREE right = (pANTLR3_BASE_TREE)(ptr->getChild(ptr, 1)); //emerson_createTreeMirrorImage( (pANTLR3_BASE_TREE)(ptr->getChild(ptr, 0))); //emerson_createTreeMirrorImage( (pANTLR3_BASE_TREE)(ptr->getChild(ptr, 1)) ); ptr->setChild(ptr, 1, ptr->getChild(ptr, 0)); ptr->setChild(ptr, 0, right); } } }
void SCsTranslator::processAttrsIdtfList(bool ignore_first, pANTLR3_BASE_TREE node, sElement *el_obj, const String &connector, bool generate_order) { sc_type type_connector = _getTypeByConnector(connector); tElementSet var_attrs, const_attrs; tElementSet subjects; uint32 n = node->getChildCount(node); uint32 idx = 1; for (uint32 i = ignore_first ? 1 : 0; i < n; ++i) { pANTLR3_BASE_TREE child = (pANTLR3_BASE_TREE)node->getChild(node, i); pANTLR3_COMMON_TOKEN tok = child->getToken(child); assert(tok); // skip internal sentences if (tok->type == SEP_LINT) continue; if (tok->type == SEP_ATTR_CONST || tok->type == SEP_ATTR_VAR) { if (!subjects.empty()) { GENERATE_ATTRS(idx) subjects.clear(); const_attrs.clear(); var_attrs.clear(); } pANTLR3_BASE_TREE nd = (pANTLR3_BASE_TREE)child->getChild(child, 0); sElement *el = _addNode(GET_NODE_TEXT(nd)); if (tok->type == SEP_ATTR_CONST) const_attrs.insert(el); else var_attrs.insert(el); } else { subjects.insert(parseElementTree(child)); } } GENERATE_ATTRS(idx) }
/** Transform ^(nil x) to x */ static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root) { pANTLR3_BASE_TREE saveRoot; // Keep track of the root we are given. If it is a nilNode, then we // can reuse it rather than orphaning it! // saveRoot = root; if (root != NULL && root->isNilNode(root)) { if (root->getChildCount(root) == 0) { root = NULL; } else if (root->getChildCount(root) == 1) { root = (pANTLR3_BASE_TREE)root->getChild(root, 0); root->setParent(root, NULL); root->setChildIndex(root, -1); // The root we were given was a nil node, wiht one child, which means it has // been abandoned and would be lost in the node factory. However // nodes can be flagged as resuable to prevent this terrible waste // saveRoot->reuse(saveRoot); } } return root; }
ObjectInitValueDef::ObjectInitValueDef(const pANTLR3_BASE_TREE node) : ValueDef(OBJECT_INIT, node) { assert(node->getType(node) == N_OBJECT_INIT_VAL); assert(node->getChildCount(node) == 2); pANTLR3_BASE_TREE n, c; { // class name n = (pANTLR3_BASE_TREE) node->getChild(node, 0); assert(n->getType(n) == N_CLASS_NAME); assert(n->getChildCount(n) == 1); n = (pANTLR3_BASE_TREE) n->getChild(n, 0); assert(n->getType(n) == ID); wchar_t* szClassName = (wchar_t*) n->getText(n)->chars; m_ClassName = szClassName; } { m_ActualParamsMap.clear(); // actual params n = (pANTLR3_BASE_TREE) node->getChild(node, 1); assert(n->getType(n) == N_ACTUAL_PARAMS); int childCount = n->getChildCount(n); for (int i = 0; i < childCount; i++) { c = (pANTLR3_BASE_TREE) n->getChild(n, i); assert(c->getType(c) == N_ACTUAL_PARAM); ActualParamDefPtr pActualParamDef(new ActualParamDef(c)); m_ActualParamsMap[pActualParamDef->getParamName()] = pActualParamDef; } } }
static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree) { pANTLR3_COMMON_TREE cTree; pANTLR3_COMMON_TOKEN token; cTree = (pANTLR3_COMMON_TREE)(tree->super); token = cTree->token; if (token == NULL || token->getLine(token) == 0) { if (tree->getChildCount(tree) > 0) { pANTLR3_BASE_TREE child; child = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); return child->getLine(child); } return 0; } return token->getLine(token); }
static pANTLR3_STRING toString (pANTLR3_BASE_TREE tree) { if (tree->isNilNode(tree) == ANTLR3_TRUE) { pANTLR3_STRING nilNode; nilNode = tree->strFactory->newPtr(tree->strFactory, (pANTLR3_UINT8)"nil", 3); return nilNode; } return ((pANTLR3_COMMON_TREE)(tree->super))->token->getText(((pANTLR3_COMMON_TREE)(tree->super))->token); }
~CompilerImpl() noexcept { if(this->compiler){ this->compiler->free(this->compiler); this->compiler = nullptr; } if(this->stream){ stream->free(this->stream); this->stream = nullptr; } if(this->tree && tree->free){ tree->free(tree); this->tree = nullptr; } }
static void toStringWork (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE p, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf) { ANTLR3_UINT32 n; ANTLR3_UINT32 c; if (!p->isNilNode(p) ) { pANTLR3_STRING text; text = p->toString(p); if (text == NULL) { text = tns->ctns->stringFactory->newRaw(tns->ctns->stringFactory); text->addc (text, ' '); text->addi (text, p->getType(p)); } buf->appendS(buf, text); } if (p == stop) { return; /* Finished */ } n = p->getChildCount(p); if (n > 0 && ! p->isNilNode(p) ) { buf->addc (buf, ' '); buf->addi (buf, ANTLR3_TOKEN_DOWN); } for (c = 0; c<n ; c++) { pANTLR3_BASE_TREE child; child = p->getChild(p, c); tns->toStringWork(tns, child, stop, buf); } if (n > 0 && ! p->isNilNode(p) ) { buf->addc (buf, ' '); buf->addi (buf, ANTLR3_TOKEN_UP); } }
/// Walk tree with depth-first-search and fill nodes buffer. /// Don't add in DOWN, UP nodes if the supplied tree is a list (t is isNilNode) // such as the root tree is. /// static void fillBuffer(pANTLR3_COMMON_TREE_NODE_STREAM ctns, pANTLR3_BASE_TREE t) { ANTLR3_BOOLEAN nilNode; ANTLR3_UINT32 nCount; ANTLR3_UINT32 c; nilNode = ctns->adaptor->isNilNode(ctns->adaptor, t); // If the supplied node is not a nil (list) node then we // add in the node itself to the vector // if (nilNode == ANTLR3_FALSE) { ctns->nodes->add(ctns->nodes, t, NULL); } // Only add a DOWN node if the tree is not a nil tree and // the tree does have children. // nCount = t->getChildCount(t); if (nilNode == ANTLR3_FALSE && nCount>0) { ctns->addNavigationNode(ctns, ANTLR3_TOKEN_DOWN); } // We always add any children the tree contains, which is // a recursive call to this function, which will cause similar // recursion and implement a depth first addition // for (c = 0; c < nCount; c++) { fillBuffer(ctns, ctns->adaptor->getChild(ctns->adaptor, t, c)); } // If the tree had children and was not a nil (list) node, then we // we need to add an UP node here to match the DOWN node // if (nilNode == ANTLR3_FALSE && nCount > 0) { ctns->addNavigationNode(ctns, ANTLR3_TOKEN_UP); } }
SCsTranslator::eSentenceType SCsTranslator::determineSentenceType(pANTLR3_BASE_TREE node) { pANTLR3_COMMON_TOKEN tok = node->getToken(node); assert(tok); if (tok->type == SEP_SIMPLE) return SentenceLevel1; if (tok->type == CONNECTORS) return SentenceLevel2_7; if (tok->type == SEP_ASSIGN) return SentenceAssign; if (tok->type == EOF) return SentenceEOF; return SentenceUnknown; }