[cedet-semantic] Using Semantic to parse XPATH
Brought to you by:
zappo
From: Alex S. <ken...@ya...> - 2001-12-08 19:05:48
|
Hi, I'm writing an XPATH implementation and I'm wondering wether using semantic would help me. The language seems quite simple so if it is a lot of overhead, I might be better off writing the parser myself. Can you help me decide that? Here are some examples using the language, from the spec itself: * child::para[position()=1] selects the first para child of the context node * child::para[position()=last()] selects the last para child of the context node * child::para[position()=last()-1] selects the last but one para child of the context node * child::para[position()>1] selects all the para children of the context node other than the first para child of the context node * following-sibling::chapter[position()=1] selects the next chapter sibling of the context node * preceding-sibling::chapter[position()=1] selects the previous chapter sibling of the context node * /descendant::figure[position()=42] selects the forty-second figure element in the document * /child::doc/child::chapter[position()=5]/child::section[position()=2] selects the second section of the fifth chapter of the doc document element * child::para[attribute::type="warning"] selects all para children of the context node that have a type attribute with value warning * child::para[attribute::type='warning'][position()=5] selects the fifth para child of the context node that has a type attribute with value warning The spec contains EBNF. Here's some examples from the spec. [1] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? | AbbreviatedAbsoluteLocationPath [3] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath [8] Predicate ::= '[' PredicateExpr ']' [9] PredicateExpr ::= Expr [14] Expr ::= OrExpr [15] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall [24] RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr | RelationalExpr '<=' AdditiveExpr | RelationalExpr '>=' AdditiveExpr I already have code to do single steps in all of this. Here are some (tested and working) examples; using M-x occur in my source file. :) 88:(defun xpath-ancestor-axis (node) 103:(defun xpath-ancestor-or-self-axis (node) 112:(defun xpath-attribute-axis (node) 120:(defun xpath-child-axis (node) 127:(defun xpath-descendant-axis (node) 145:(defun xpath-descendant-or-self-axis (node) 153:(defun xpath-following-axis (node) 181:(defun xpath-following-sibling-axis (node) 190:(defun xpath-parent-axis (node) 198:(defun xpath-preceding-axis (node) 224:(defun xpath-preceding-sibling-axis (node) 242:(defun xpath-self-axis (node) 249:(defun xpath-name-filter (node-list name) 257:(defun xpath-text-filter (node-list) 297:(defun xpath-last-function () 302:(defun xpath-position-function () 307:(defun xpath-count-function (node-set) 311:(defun xpath-name-function (&optional node-set) Thus I am reasonably sure that I can implement something like the following example. The predicates in the square brackets can become very complex, however. This is where I wonder wether Semantic will help. /child::doc/child::chapter[position()=5]/child::section[position()=2] If it helps for the predicates, of course, then I'd want to use if for the rest of XPATH as well. What do you think? Alex. -- http://www.geocities.com/kensanata/ Coffee should be black as hell, strong as death and sweet as love. -- Turkish proverb |