summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b221fb7)
raw | patch | inline | side by side (parent: b221fb7)
author | ishmal <ishmal@users.sourceforge.net> | |
Sun, 9 Jul 2006 22:06:05 +0000 (22:06 +0000) | ||
committer | ishmal <ishmal@users.sourceforge.net> | |
Sun, 9 Jul 2006 22:06:05 +0000 (22:06 +0000) |
src/dom/xpathparser.cpp | patch | blob | history | |
src/dom/xpathtoken.cpp | patch | blob | history | |
src/dom/xpathtoken.h | patch | blob | history |
index 5962b70dc68294b28e5117a87d5dbb8dfec63c66..cb06a0fb263d9e145e4b6812e11f102b7433484a 100644 (file)
--- a/src/dom/xpathparser.cpp
+++ b/src/dom/xpathparser.cpp
//### Execute the token list
TokenExecutor executor;
- list = executor.execute(tokens, root);
+ NodeList results;
+ if (!executor.execute(tokens, root, results))
+ {
+ //error
+ }
- return list;
+ return results;
}
diff --git a/src/dom/xpathtoken.cpp b/src/dom/xpathtoken.cpp
index cf51bf9ba9d384711ef7e650cc93a94e1848a12b..f08dfe7e3b7498b4d586d4b5d8e5d98499eb7df8 100644 (file)
--- a/src/dom/xpathtoken.cpp
+++ b/src/dom/xpathtoken.cpp
//# V A L U E S
//###########################
-static bool tokStr(Token &tok, TokenExecutor &exec)
+static bool tokStr(Token &tok, Stack &stack)
{
StackItem item;
item.sval = tok.sval;
- exec.push(item);
+ stack.push(item);
return true;
}
-static bool tokFloat(Token &tok, TokenExecutor &exec)
+static bool tokFloat(Token &tok, Stack &stack)
{
StackItem item;
item.dval = tok.dval;
- exec.push(item);
+ stack.push(item);
return true;
}
-static bool tokInt(Token &tok, TokenExecutor &exec)
+static bool tokInt(Token &tok, Stack &stack)
{
StackItem item;
item.ival = tok.ival;
- exec.push(item);
+ stack.push(item);
return true;
}
-static bool tokAnd(Token &tok, TokenExecutor &exec)
+static bool tokAnd(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = item1.ival && item2.ival;
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokOr(Token &tok, TokenExecutor &exec)
+static bool tokOr(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = item1.ival || item2.ival;
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokMod(Token &tok, TokenExecutor &exec)
+static bool tokMod(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.dval = fmod(item1.dval, item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokDiv(Token &tok, TokenExecutor &exec)
+static bool tokDiv(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.dval /= item2.dval;
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokMul(Token &tok, TokenExecutor &exec)
+static bool tokMul(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.dval *= item2.dval;
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokPlus(Token &tok, TokenExecutor &exec)
+static bool tokPlus(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.dval += item2.dval;
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokMinus(Token &tok, TokenExecutor &exec)
+static bool tokMinus(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.dval -= item2.dval;
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokNeg(Token &tok, TokenExecutor &exec)
+static bool tokNeg(Token &tok, Stack &stack)
{
- StackItem item = exec.pop();
+ StackItem item = stack.pop();
item.dval = -item.dval;
item.ival = -item.ival;
- exec.push(item);
+ stack.push(item);
return true;
}
-static bool tokEquals(Token &tok, TokenExecutor &exec)
+static bool tokEquals(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = (item1.dval == item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokNotEquals(Token &tok, TokenExecutor &exec)
+static bool tokNotEquals(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = (item1.dval != item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokLessThan(Token &tok, TokenExecutor &exec)
+static bool tokLessThan(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = (item1.dval < item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokLessThanEquals(Token &tok, TokenExecutor &exec)
+static bool tokLessThanEquals(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = (item1.dval <= item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokGreaterThanEquals(Token &tok, TokenExecutor &exec)
+static bool tokGreaterThanEquals(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = (item1.dval >= item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
-static bool tokGreaterThan(Token &tok, TokenExecutor &exec)
+static bool tokGreaterThan(Token &tok, Stack &stack)
{
- StackItem item1 = exec.pop();
- StackItem item2 = exec.pop();
+ StackItem item1 = stack.pop();
+ StackItem item2 = stack.pop();
item1.ival = (item1.dval > item2.dval);
- exec.push(item1);
+ stack.push(item1);
return true;
}
//# X P A T H I T E M S
//###########################
-static bool tokAbsolute(Token &tok, TokenExecutor &exec)
+static bool tokAbsolute(Token &tok, Stack &stack)
{
return true;
}
-static bool tokRelative(Token &tok, TokenExecutor &exec)
+static bool tokRelative(Token &tok, Stack &stack)
{
return true;
}
-static bool tokStep(Token &tok, TokenExecutor &exec)
+static bool tokStep(Token &tok, Stack &stack)
{
return true;
}
-static bool tokNameTest(Token &tok, TokenExecutor &exec)
+static bool tokNameTest(Token &tok, Stack &stack)
{
return true;
}
-static bool tokExpr(Token &tok, TokenExecutor &exec)
+static bool tokExpr(Token &tok, Stack &stack)
{
return true;
}
-static bool tokUnion(Token &tok, TokenExecutor &exec)
+static bool tokUnion(Token &tok, Stack &stack)
{
return true;
}
//###########################
-static bool tokAxisAncestorOrSelf(Token &tok, TokenExecutor &exec)
+static bool tokAxisAncestorOrSelf(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisAncestor(Token &tok, TokenExecutor &exec)
+static bool tokAxisAncestor(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisAttribute(Token &tok, TokenExecutor &exec)
+static bool tokAxisAttribute(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisChild(Token &tok, TokenExecutor &exec)
+static bool tokAxisChild(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisDescendantOrSelf(Token &tok, TokenExecutor &exec)
+static bool tokAxisDescendantOrSelf(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisDescendant(Token &tok, TokenExecutor &exec)
+static bool tokAxisDescendant(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisFollowingSibling(Token &tok, TokenExecutor &exec)
+static bool tokAxisFollowingSibling(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisFollowing(Token &tok, TokenExecutor &exec)
+static bool tokAxisFollowing(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisNamespace(Token &tok, TokenExecutor &exec)
+static bool tokAxisNamespace(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisParent(Token &tok, TokenExecutor &exec)
+static bool tokAxisParent(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisPrecedingSibling(Token &tok, TokenExecutor &exec)
+static bool tokAxisPrecedingSibling(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisPreceding(Token &tok, TokenExecutor &exec)
+static bool tokAxisPreceding(Token &tok, Stack &stack)
{
return true;
}
-static bool tokAxisSelf(Token &tok, TokenExecutor &exec)
+static bool tokAxisSelf(Token &tok, Stack &stack)
{
return true;
}
//###########################
-static bool tokFuncLast(Token &tok, TokenExecutor &exec)
+static bool tokFuncLast(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncPosition(Token &tok, TokenExecutor &exec)
+static bool tokFuncPosition(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncCount(Token &tok, TokenExecutor &exec)
+static bool tokFuncCount(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncId(Token &tok, TokenExecutor &exec)
+static bool tokFuncId(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncLocalName(Token &tok, TokenExecutor &exec)
+static bool tokFuncLocalName(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncNamespaceUri(Token &tok, TokenExecutor &exec)
+static bool tokFuncNamespaceUri(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncName(Token &tok, TokenExecutor &exec)
+static bool tokFuncName(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncString(Token &tok, TokenExecutor &exec)
+static bool tokFuncString(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncConcat(Token &tok, TokenExecutor &exec)
+static bool tokFuncConcat(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncStartsWith(Token &tok, TokenExecutor &exec)
+static bool tokFuncStartsWith(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncContains(Token &tok, TokenExecutor &exec)
+static bool tokFuncContains(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncSubstringBefore(Token &tok, TokenExecutor &exec)
+static bool tokFuncSubstringBefore(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncSubstringAfter(Token &tok, TokenExecutor &exec)
+static bool tokFuncSubstringAfter(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncSubstring(Token &tok, TokenExecutor &exec)
+static bool tokFuncSubstring(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncStringLength(Token &tok, TokenExecutor &exec)
+static bool tokFuncStringLength(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncNormalizeSpace(Token &tok, TokenExecutor &exec)
+static bool tokFuncNormalizeSpace(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncTranslate(Token &tok, TokenExecutor &exec)
+static bool tokFuncTranslate(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncBoolean(Token &tok, TokenExecutor &exec)
+static bool tokFuncBoolean(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncNot(Token &tok, TokenExecutor &exec)
+static bool tokFuncNot(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncTrue(Token &tok, TokenExecutor &exec)
+static bool tokFuncTrue(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncFalse(Token &tok, TokenExecutor &exec)
+static bool tokFuncFalse(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncLang(Token &tok, TokenExecutor &exec)
+static bool tokFuncLang(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncNumber(Token &tok, TokenExecutor &exec)
+static bool tokFuncNumber(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncSum(Token &tok, TokenExecutor &exec)
+static bool tokFuncSum(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncFloor(Token &tok, TokenExecutor &exec)
+static bool tokFuncFloor(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncCeiling(Token &tok, TokenExecutor &exec)
+static bool tokFuncCeiling(Token &tok, Stack &stack)
{
return true;
}
-static bool tokFuncRound(Token &tok, TokenExecutor &exec)
+static bool tokFuncRound(Token &tok, Stack &stack)
{
return true;
}
void TokenExecutor::assign(const TokenExecutor &other)
{
tokenList = other.tokenList;
- stack = other.stack;
}
*/
void TokenExecutor::reset()
{
- stack.clear();
}
/**
- * Set the root node
+ * Execute a list upon a given node. For each Axis encountered,
+ * get the nodes encountered so far, and execute the rest of the
+ * list of tokens upon each of the nodes.
*/
-NodeList TokenExecutor::execute(const TokenList &tokens, const Node *node)
+int TokenExecutor::execute(std::vector<Token> &tokens,
+ int position,
+ const Node *node,
+ NodeList &result)
{
+ Stack stack(*this);
- nodeList.clear();
+ NodeList contextNodes;
- return nodeList;
-}
+ int length = (int) tokens.size();
+ for ( ; position < length ; position ++)
+ {
+ Token tok = tokens[position];
+ if (tok.isAxis())
+ {
+ int pos2 = 0;
+ //Do rest of tokens with the nodes we have found so far
+ for (unsigned int i = 0 ; i<contextNodes.getLength() ; i++)
+ {
+ Node *contextNode = contextNodes.item(i);
+ pos2 = execute(tokens, position, contextNode, result);
+ if (pos2 < 0)
+ {
+ return -1;
+ }
+ }
+ position = pos2;
+ }
+ else
+ {
+ if (!tok.execute(stack))
+ {
+ return -1;
+ }
+ }
+ }//for
+ return position;
+}
/**
- *
+ * Execute the tokens in a TokenList upon a given node
*/
-void TokenExecutor::push(StackItem &item)
+bool TokenExecutor::execute(TokenList &tokenList,
+ const Node *node,
+ NodeList &result)
{
- stack.push_back(item);
-}
-/**
- *
- */
-StackItem TokenExecutor::pop()
-{
- if (stack.size()<1)
+ NodeList nodelist;
+
+ std::vector<Token> &tokens = tokenList.getTokens();
+ if (execute(tokens, 0, node, nodeList) < 0)
{
- StackItem item;
- return item;
+ //error message
+ return false;
}
- std::vector<StackItem>::iterator iter = stack.end()-1;
- StackItem item = *iter;
- stack.erase(iter);
- return item;
+
+ result = nodeList;
+
+ return true;
}
+
+
+
} // namespace xpath
} // namespace dom
} // namespace w3c
diff --git a/src/dom/xpathtoken.h b/src/dom/xpathtoken.h
index 26aee60f82f040f6be7aca23b945b62fc571169a..61d6cf6efb44228fbf64ca402fc98657d68163b4 100644 (file)
--- a/src/dom/xpathtoken.h
+++ b/src/dom/xpathtoken.h
-/**
- * Contains the StackItem stack;
- */
-typedef std::vector<StackItem> Stack;
-
-
//########################################################################
//# X P A T H T O K E N
//########################################################################
class Token;
-class TokenExecutor;
+class Stack;
-typedef bool (*TokenFunc)(Token &tok, TokenExecutor &exec);
+typedef bool (*TokenFunc)(Token &tok, Stack &stack);
/**
*/
virtual int getType()
{ return type; }
+
+ /**
+ * Return true if the type is one of the Axis types
+ * above;
+ */
+ virtual bool isAxis()
+ {
+ return type>=TOK_AXIS_ANCESTOR_OR_SELF &&
+ type <= TOK_AXIS_SELF;
+ }
/**
* Return the string TokenType of this token
*/
* Let this token execute itself on the given stack,
* possibly adding Nodes to the node list.
*/
- virtual bool execute(TokenExecutor &exec)
+ virtual bool execute(Stack &stack)
{
if (tokenFunc)
- return tokenFunc(*this, exec);
+ return tokenFunc(*this, stack);
return false;
}
//########################################################################
-//# C O N T E X T
+//# S T A C K
//########################################################################
/**
*
*/
-class Context
+class Stack
{
public:
/**
* Constructor
*/
- Context()
- { init(); }
-
- /**
- * Constructor
- */
- Context(int principalNodeTypeArg)
- { init(); principalNodeType = principalNodeTypeArg; }
+ Stack(TokenExecutor &par) : parent(par)
+ { clear(); }
/**
* Copy constructor
*/
- Context(const Context &other)
+ Stack(const Stack &other) : parent(other.parent)
{ assign(other); }
/**
* Destructor
*/
- virtual ~Context()
+ virtual ~Stack()
{}
/**
*
*/
- Context &operator=(const Context &other)
+ Stack &operator=(const Stack &other)
{ assign(other); return *this; }
/**
*
*/
- std::vector<Token> &getTokens()
- { return tokens; }
-
- /**
- *
- */
- void setTokens(const std::vector<Token> &tokensArg)
- { tokens = tokensArg; }
+ void push(StackItem &item)
+ { return stackItems.push_back(item); }
/**
*
*/
- void add(const Token &token)
- { tokens.push_back(token); }
-
- /**
- *
- */
- virtual void dump()
+ StackItem pop()
{
- for (unsigned int i=0 ; i<tokens.size() ; i++)
+ if (stackItems.size() < 1)
{
- Token token = tokens[i];
- token.dump();
+ //TODO: error here
+ StackItem item;
+ return item;
}
+ std::vector<StackItem>::iterator iter =
+ stackItems.end() - 1;
+ StackItem item = *iter;
+ stackItems.erase(iter);
+ return item;
}
/**
*/
virtual void clear()
{
- tokens.clear();
+ stackItems.clear();
+ principalNodeType = AXIS_ELEMENT;
}
private:
- void init()
- {
- principalNodeType = AXIS_ELEMENT;
- }
-
- void assign(const Context &other)
+ void assign(const Stack &other)
{
principalNodeType = other.principalNodeType;
- tokens = other.tokens;
+ stackItems = other.stackItems;
}
int principalNodeType;
- std::vector<Token> tokens;
+ std::vector<StackItem> stackItems;
+
+ TokenExecutor &parent;
};
+
+
//########################################################################
//# T O K E N L I S T
//########################################################################
virtual ~TokenList()
{ }
+
/**
*
*/
- virtual void add(const Context &contextArg)
- {
- contexts.push_back(currentContext);
- currentContext=contextArg;
- }
+ virtual void add(const Token &token)
+ { tokens.push_back(token); }
/**
*
*/
- virtual void add(const Token &token)
- { currentContext.add(token); }
+ virtual std::vector<Token> &getTokens()
+ { return tokens; }
/**
*
*/
virtual void dump()
{
- for (unsigned int i=0 ; i<contexts.size() ; i++)
+ for (unsigned int i=0 ; i<tokens.size() ; i++)
{
- Context context = contexts[i];
- context.dump();
+ Token token = tokens[i];
+ token.dump();
}
}
*/
virtual void clear()
{
- currentContext.clear();
- contexts.clear();
+ tokens.clear();
}
private:
void assign(const TokenList &other)
{
- currentContext = other.currentContext;
- contexts = other.contexts;
+ tokens = other.tokens;
}
- Context currentContext;
-
- std::vector<Context> contexts;
+ std::vector<Token> tokens;
};
virtual void reset();
/**
- * Push a stack item onto the stack
+ * Execute a list upon a given node. For each Axis encountered,
+ * get the nodes encountered so far, and execute the rest of the
+ * list of tokens upon each of the nodes.
*/
- virtual void push(StackItem &item);
-
- /**
- * Pop a stack item from the stack
- */
- virtual StackItem pop();
+ int execute(std::vector<Token> &tokens,
+ int position,
+ const Node *node,
+ NodeList &nodeList);
/**
* Execute a token list on the stack
*/
- NodeList execute(const TokenList &list, const Node *node);
+ bool execute(TokenList &list,
+ const Node *node,
+ NodeList &result);
private:
- /**
- * Contains the StackItem stack;
- */
- Stack stack;
-
-
/**
*
*/