221 lines · 7.8 KB · Text
RawDownload
= w3c-xpointers: parser for XPointers framework including XPath     include "xpointers.m";     xpointers := load Xpointers Xpointers->PATH;    Xpath, Xstep: import xpointers;        # special operators ('+', '-', etc represent themselves)    One, Ole, Oge, Omul, Odiv, Omod, Oand, Oor, Oneg,    Onodetype, Onametest, Ofilter, Opath: con ...;        # axis types    Aancestor,    Aancestor_or_self,    Aattribute,    Achild,    Adescendant,    Adescendant_or_self,    Afollowing,    Afollowing_sibling,    Anamespace,    Aparent,    Apreceding,    Apreceding_sibling,    Aself: con iota;        Xstep: adt {       axis:  int;  # Aancestor, ... (above)       op:    int;  # Onametest or Onodetype       ns:    string;       name:  string;       arg:   string;  # optional parameter to processing-instruction       preds: cyclic list of ref Xpath;           text:  fn(nil: self ref Xstep): string;       axisname:  fn(i: int): string;    };        Xpath: adt {       pick{       E =>          op:    int;          l, r:  cyclic ref Xpath;       Fn =>          ns:    string;          name:  string;          args:  cyclic list of ref Xpath;       Var =>          ns:    string;          name:  string;       Path =>          abs:   int;          steps: list of ref Xstep;       Int =>          val:   big;       Real =>          val:   real;       Str =>          s:     string;       }       text: fn(nil: self ref Xpath): string;    };        framework:  fn(s: string):                   (string, list of (string, string, string), string);        # predefined schemes    element:    fn(s: string): (string, list of int, string);    xmlns:      fn(s: string): (string, string, string);    xpointer:   fn(s: string): (ref Xpath, string);    Xpointers implements a parser for the World-Wide Web  Consor‐tium's  XPointers  framework,  including  a  parser for XPathexpressions. Init must be called before any other operation in the module. Framework parses a string s according to the grammar for  theXPointers  framework,  and  returns  a  tuple  (short, point‐ers, err).  On an error, the string err  gives  a  diagnosticand  the  other  two  values are nil.  Otherwise, if short isnon-nil, the XPointer was a  `shorthand  pointer',  with  thegiven value; pointers will be nil.  If a scheme-based pointeris used, short is nil and  pointers  has  a  list  of  tuples(ns, scheme, data),  each representing one pointer value.  Nsis the XML name space for the given scheme; the default  namespace  is  represented by nil.  Scheme is the XPointer schemename within that name space; and data is the  actual  pointervalue  following  the  rules  of that scheme.  (They all havecompletely different syntax.) Three common schemes are directly supported by the module, byfunctions  named  after  the  scheme.  All of them follow theconvention of returning a tuple in which the last element  isa  diagnostic string.  On an error, all but that last elementof the tuple will be nil, and the last element will be a non-nil string with a diagnostic. Xmlns parses an XML name space definition of the form ns=uri,and returns its components. Element parses a value of the XPointer element scheme,  givenby the grammar:        selector ::= name child*  |  child+       child ::= '/' [1-9][0-9]* The  optional  name is an XPointer `shorthand pointer'.  Eachchild number selects the child with that index (origin 1)  atthe  corresponding  level  of  the  XML tree beneath the nodeselected by the name, or starting at  the  root  of  the  XMLtree.  Element returns a tuple ((name, path), err) where nameis the top element name or nil if  none  was  specified,  andpath is a list of int giving the path of child indices. The  most  complex  scheme is xpointer, because its syntax isthat of XML's elaborate XPath  expression.   Xpointer  parsessuch  an  expression  and  returns  a  tuple (e, err) where erefers to an Xpath value that represents the abstract  syntaxof the XPath Expr.  Xpointer checks only the syntax of s, anddoes not check that functions are limited to those  specifiedby  the  xpointer  scheme (that is consistent with it being aparse of s, rather than an XPointer or XPath evaluator). Xpath and Xstep together represent an abstract syntax of  theXPath grammar. Xstep represents the XPath Step grammar rule, with all abbre‐viations expanded to their full form:        Step ::= AxisName '::' NodeTest Predicate*       NodeTest ::= NameTest | NodeType '(' ')'       NameTest ::= '*' | NCName ':' '*' | (NCName ':')? NCName       Predicate ::= '[' Expr ']' The correspondence is as follows: s.axis Represents  the  AxisName  by  one  of  the  constants       Aancestor to Aself. s.op   Onametest  or  Onodetype  to  say which rule is repre‐       sented s.ns   For a NameTest, gives the XML name space; can  be  for       `any  name  space'  or nil for the default name space.       For a NodeType, gives the type:  comment,  node,  pro‐       cessing-instruction, or text. s.name Gives the name for a NameTest; can be for `any name'. s.arg  The optional literal parameter to a NodeType that is a       processing-instruction. s.preds       A list  of  Xpath  values  representing  the  optional       sequence of Predicate expressions s.text()       Returns  a  string  representing  the Xstep in textual       form. s.axisname(a)       Returns the printable text for axis code a (ie, one of       Aancestor to Aself) Xpath  values  represent  an  abstract  syntax  for  an XPathexpression.  Briefly, an expression follows the grammar below(see the XPath specification for the full concrete syntax).        e ::=  e 'or' e          |   e 'and' e          |   e ('=' | '!=') e          |   e ('<' | '<=' | '>=' | '>') e          |   e ('+' | '-') e          |   e ('*' | 'div' | 'mod') e          |   '-' e          |   e '|' e          |   filter          |   path       filter ::= primary predicate* (('/' | '//') relpath)?       primary ::= '$' QName | '(' e ')' | Literal | Number | FunctionName '(' (e (',' e)*) ')'       path ::= '/' relpath | relpath       relpath ::= relpath '/' relpath | relpath '//' relpath | Step Most  of  e  is  represented  by a binary tree using the pickXpath.E(op, l, r) where op is an operator symbol (either  thecharacter  itself or one of the constants One, Odiv, etc. forcompound symbols), and l and r represent the  operands.   Theonly unary operator Oneg has its operand in l.  A filter usesthe binary operator Xpath.E(Ofilter, e, pred) to  apply  eachpredicate  to  the  preceding primary or predicate.  A filteralso uses Xpath.E(Opath, e, relpath) to  apply  the  optionalrelpath (represented by a value of Xpath.Path) to the preced‐ing part of the filter expression. The other cases in the pick adt  correspond  to  the  variouschoices  of  path  and primary.  Integer and real numbers aredistinguished.  Literal is represented by Xpath.Str; variablereferences  (ie,  $QName) are represented by XPath.Var, wherens gives the optional XML name space of the  name.   Path  isrepresented  by  Xpath.Path(abs, steps) where abs is non-zeroif and only if the path  is  absolute  (starts  with  `/'  or`//'),  and steps lists the Xstep values corresponding to theslash-separated Steps in the grammar.  Abbreviated forms suchas `//' are converted by xpointer to their full internal formin terms of `/', as defined by the specification, so there isno need to distinguish the delimiters in this representation. == source/appl/lib/w3c/xpointers.b == see ALSO``XML     Path     Language     (XPath)     Version    1.0'',http://www.w3.org/TR/xpath``XPointer framework'', http://www.w3.org/TR/xptr-framework/``XPointer element() scheme'', http://www.w3.org/TR/xptr-ele‐ment/``XPointer xmlns() scheme'', http://www.w3.org/TR/xptr-xmlns/``XPointer  xpointer()  scheme'',  http://www.w3.org/TR/xptr-xpointer/