| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427 |
- //
- // XPath2Expression.cs - abstract syntax tree for XPath 2.0
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Collections;
- using System.Globalization;
- using System.Xml;
- using System.Xml.Query;
- using System.Xml.Schema;
- using System.Xml.XPath;
- using Mono.Xml.XQuery;
- namespace Mono.Xml.XPath2
- {
- public class ExprSequence : CollectionBase
- {
- public ExprSequence ()
- {
- }
- public void Add (ExprSingle expr)
- {
- List.Add (expr);
- }
- public void AddRange (ICollection items)
- {
- if (items != null)
- foreach (ExprSingle e in items)
- List.Add (e);
- }
- public void Insert (int pos, ExprSingle expr)
- {
- List.Insert (pos, expr);
- }
- public ExprSingle this [int i] {
- get { return List [i] as ExprSingle; }
- set { List [i] = value; }
- }
- internal void CheckReference (XQueryASTCompiler compiler)
- {
- foreach (ExprSingle expr in List)
- expr.CheckReference (compiler);
- }
- }
- public abstract partial class ExprSingle
- {
- internal abstract void CheckReference (XQueryASTCompiler compiler);
- #region CompileAndEvaluate
- internal static readonly XPathAtomicValue AtomicTrue = new XPathAtomicValue (true, XmlSchemaSimpleType.XsBoolean);
- internal static readonly XPathAtomicValue AtomicFalse = new XPathAtomicValue (false, XmlSchemaSimpleType.XsBoolean);
- XQueryStaticContext ctx;
- internal ExprSingle Compile (XQueryASTCompiler compiler)
- {
- this.ctx = ctx;
- return CompileCore (compiler);
- }
- // If internal&&protected is available in C#, it is the best signature.
- internal abstract ExprSingle CompileCore (XQueryASTCompiler compiler);
- internal XQueryStaticContext Context {
- get { return ctx; }
- }
- public abstract SequenceType StaticType { get; }
- /** <summary>
- This is the core part of ExprSingle. It is
- generally used to evaluate expression and returns
- XPathItem sequence (iterator). The result is unordered
- */
- public abstract XPathSequence Evaluate (XPathSequence iter);
- public virtual XPathSequence EvaluateOrdered (XPathSequence iter)
- {
- if (RequireSorting) {
- ArrayList al = new ArrayList ();
- foreach (XPathItem item in Evaluate (iter))
- al.Add (item);
- return new ListIterator (iter.Context, al);
- }
- else
- return Evaluate (iter);
- }
- public virtual void Serialize (XPathSequence iter)
- {
- XmlWriter w = iter.Context.Writer;
- XPathSequence result = Evaluate (iter);
- bool initial = true;
- foreach (XPathItem item in result) {
- if (initial)
- initial = false;
- else
- w.WriteWhitespace (" ");
- WriteXPathItem (item, w);
- }
- }
- private void WriteXPathItem (XPathItem item, XmlWriter w)
- {
- if (item.IsNode) {
- XPathNavigator nav = item as XPathNavigator;
- if (w.WriteState != WriteState.Start && nav.NodeType == XPathNodeType.Root)
- throw new XmlQueryException ("Current output can not accept root node.");
- if (w.WriteState == WriteState.Attribute)
- w.WriteString (nav.Value);
- else
- w.WriteNode (nav, false);
- } else
- w.WriteString (item.Value);
- }
- // get EBV (fn:boolean())
- public virtual bool EvaluateAsBoolean (XPathSequence iter)
- {
- XPathSequence result = Evaluate (iter);
- if (!result.MoveNext ())
- return false;
- XPathItem v = result.Current;
- if (v is XPathNavigator)
- return true;
- if (result.MoveNext ())
- return true;
- switch (v.XmlType.TypeCode) {
- case XmlTypeCode.Boolean:
- return v.ValueAsBoolean;
- case XmlTypeCode.String:
- case XmlTypeCode.UntypedAtomic:
- return v.Value != String.Empty;
- case XmlTypeCode.Float:
- return v.ValueAsSingle != Single.NaN && v.ValueAsSingle != 0.0;
- case XmlTypeCode.Double:
- return v.ValueAsDouble != Double.NaN && v.ValueAsSingle != 0.0;
- case XmlTypeCode.Decimal:
- return v.ValueAsDecimal != 0;
- case XmlTypeCode.Integer:
- case XmlTypeCode.NonPositiveInteger:
- case XmlTypeCode.NegativeInteger:
- case XmlTypeCode.Long:
- case XmlTypeCode.Int:
- case XmlTypeCode.Short:
- case XmlTypeCode.Byte:
- case XmlTypeCode.UnsignedInt:
- case XmlTypeCode.UnsignedShort:
- case XmlTypeCode.UnsignedByte:
- return v.ValueAsInt64 != 0;
- case XmlTypeCode.NonNegativeInteger:
- case XmlTypeCode.UnsignedLong:
- case XmlTypeCode.PositiveInteger:
- return (ulong) (v.ValueAs (typeof (ulong))) != 0;
- }
- // otherwise, return true
- return true;
- }
- public virtual int EvaluateAsInt (XPathSequence iter)
- {
- XPathAtomicValue v = Atomize (Evaluate (iter));
- return v != null ? v.ValueAsInt32 : 0;
- }
- public virtual string EvaluateAsString (XPathSequence iter)
- {
- XPathAtomicValue v = Atomize (Evaluate (iter));
- return v != null ? v.Value : String.Empty;
- }
- public static XPathAtomicValue Atomize (XPathItem item)
- {
- XPathNavigator nav = item as XPathNavigator;
- if (nav != null) {
- if (nav.SchemaInfo != null)
- return new XPathAtomicValue (nav.TypedValue, nav.SchemaInfo.SchemaType);
- else
- return new XPathAtomicValue (nav.Value, XmlSchemaSimpleType.XdtUntypedAtomic);
- }
- else
- return (XPathAtomicValue) item;
- }
- // FIXME: What if iter contains list value?
- public static XPathAtomicValue Atomize (XPathSequence iter)
- {
- if (!iter.MoveNext ())
- return null;
- XPathNavigator nav = iter.Current as XPathNavigator;
- if (nav != null) {
- // FIXME: is it really always untypedAtomic?
- // It might be complex content.
- XmlSchemaType type = nav.SchemaInfo == null ? XmlSchemaSimpleType.XdtUntypedAtomic : nav.SchemaInfo.SchemaType;
- return new XPathAtomicValue (nav.TypedValue, type);
- }
- else
- return (XPathAtomicValue) iter.Current;
- }
- public virtual XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
- {
- return Atomize (Evaluate (iter));
- }
- public virtual bool RequireSorting {
- get { return false; }
- }
- #endregion
- }
- // FLWORExpr
- internal partial class FLWORExpr : ExprSingle
- {
- public FLWORExpr (ForLetClauseCollection forlet, ExprSequence whereClause, OrderSpecList orderBy, ExprSingle ret)
- {
- this.fl = forlet;
- if (whereClause != null)
- this.whereClause = new ParenthesizedExpr (whereClause);
- this.orderBy = orderBy;
- this.ret = ret;
- }
- ForLetClauseCollection fl;
- ExprSingle whereClause;
- OrderSpecList orderBy;
- ExprSingle ret;
- public ForLetClauseCollection ForLetClauses {
- get { return fl; }
- }
- public ExprSingle WhereClause {
- get { return whereClause; }
- }
- public OrderSpecList OrderBy {
- get { return orderBy; }
- }
- public ExprSingle ReturnExpr {
- get { return ret; }
- set { ret = value; }
- }
- // ExprSingle Overrides
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- foreach (ForLetClause flc in fl)
- foreach (ForLetSingleBody single in flc)
- single.CheckReference (compiler);
- if (whereClause != null)
- whereClause.CheckReference (compiler);
- if (orderBy != null)
- foreach (OrderSpec os in orderBy)
- os.Expression.CheckReference (compiler);
- ret.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- foreach (ForLetClause flc in ForLetClauses) {
- foreach (ForLetSingleBody flsb in flc) {
- flsb.Expression = flsb.Expression.Compile (compiler);
- if (flsb.ReturnType != null)
- compiler.CheckType (flsb.Expression, flsb.ReturnType);
- }
- }
- if (WhereClause != null)
- whereClause = whereClause.Compile (compiler);
- if (OrderBy != null)
- foreach (OrderSpec os in OrderBy)
- os.Expression = os.Expression.Compile (compiler);
- ReturnExpr = ReturnExpr.Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get { return ReturnExpr.StaticType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new FLWORIterator (iter, this);
- }
- #endregion
- }
- internal class ForLetClauseCollection : CollectionBase
- {
- public void Add (ForLetClause clause)
- {
- List.Add (clause);
- }
- public void Insert (int pos, ForLetClause clause)
- {
- List.Insert (pos, clause);
- }
- public ForLetClause this [int i] {
- get { return (ForLetClause) List [i]; }
- }
- }
- internal class OrderSpecList : CollectionBase
- {
- bool isStable;
- public OrderSpecList ()
- {
- }
- public bool IsStable {
- get { return isStable; }
- set { isStable = value; }
- }
- public void Insert (int pos, OrderSpec os)
- {
- List.Insert (pos, os);
- }
- public void Add (OrderSpec spec)
- {
- List.Add (spec);
- }
- public OrderSpec this [int i] {
- get { return (OrderSpec) List [i]; }
- }
- }
- internal class OrderSpec
- {
- public OrderSpec (ExprSingle expr, OrderModifier modifier)
- {
- this.expr = expr;
- this.mod = modifier;
- }
- ExprSingle expr;
- OrderModifier mod;
- public ExprSingle Expression {
- get {return expr; }
- set { expr = value; }
- }
- public OrderModifier Modifier {
- get { return mod; }
- set { mod = value; }
- }
- }
- internal class OrderModifier
- {
- public OrderModifier (XmlSortOrder order, XmlSortOrder emptyOrder, string collation)
- {
- this.sortOrder = sortOrder;
- this.emptyOrder = emptyOrder;
- if (collation != null)
- this.coll = new CultureInfo (collation);
- }
- XmlSortOrder sortOrder;
- XmlSortOrder emptyOrder;
- CultureInfo coll;
- public XmlSortOrder SortOrder {
- get { return sortOrder; }
- }
- public XmlSortOrder EmptyOrder {
- get { return emptyOrder; }
- }
- public CultureInfo Collation {
- get { return coll; }
- }
- }
- internal class ForLetClause : CollectionBase
- {
- public ForLetSingleBody this [int i] {
- get { return (ForLetSingleBody) List [i]; }
- }
- }
- internal class ForClause : ForLetClause
- {
- public ForClause ()
- {
- }
- public void Insert (int pos, ForSingleBody body)
- {
- List.Insert (pos, body);
- }
- public void Add (ForSingleBody body)
- {
- List.Add (body);
- }
- }
- internal class LetClause : ForLetClause
- {
- public LetClause ()
- {
- }
- public void Insert (int pos, LetSingleBody body)
- {
- List.Insert (pos, body);
- }
- public void Add (LetSingleBody body)
- {
- List.Add (body);
- }
- }
- internal abstract class ForLetSingleBody
- {
- XmlQualifiedName varName;
- SequenceType type;
- ExprSingle expr;
- public ForLetSingleBody (XmlQualifiedName varName, SequenceType type, ExprSingle expr)
- {
- this.varName = varName;
- this.type = type;
- this.expr = expr;
- }
- public XmlQualifiedName VarName {
- get { return varName; }
- }
- public SequenceType ReturnType {
- get { return type; }
- }
- public ExprSingle Expression {
- get { return expr; }
- set { expr = value; }
- }
- internal void CheckReference (XQueryASTCompiler compiler)
- {
- if (type != null)
- compiler.CheckSchemaType (type);
- expr.CheckReference (compiler);
- }
- }
- internal class ForSingleBody : ForLetSingleBody
- {
- public ForSingleBody (XmlQualifiedName varName, SequenceType type, XmlQualifiedName positionalVar, ExprSingle expr)
- : base (varName, type, expr)
- {
- this.positionalVar = positionalVar;
- }
- XmlQualifiedName positionalVar;
- public XmlQualifiedName PositionalVar {
- get { return positionalVar; }
- }
- }
- internal class LetSingleBody : ForLetSingleBody
- {
- public LetSingleBody (XmlQualifiedName varName, SequenceType type, ExprSingle expr)
- : base (varName, type, expr)
- {
- }
- }
- // QuantifiedExpr
- internal class QuantifiedExpr : ExprSingle
- {
- QuantifiedExprBodyList body;
- ExprSingle satisfies;
- bool every;
- public QuantifiedExpr (bool every, QuantifiedExprBodyList body, ExprSingle satisfies)
- {
- this.every = every;
- this.body = body;
- this.satisfies = satisfies;
- }
- public bool Every {
- get { return every; }
- }
- public QuantifiedExprBodyList BodyList {
- get { return body; }
- }
- public ExprSingle Satisfies {
- get { return satisfies; }
- set { satisfies = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- foreach (QuantifiedExprBody one in body) {
- if (one.Type != null)
- compiler.CheckSchemaType (one.Type);
- one.Expression.CheckReference (compiler);
- }
- Satisfies.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Satisfies = Satisfies.Compile (compiler);
- for (int i = 0; i < BodyList.Count; i++) {
- BodyList [i].Expression = BodyList [i].Expression.Compile (compiler);
- if (BodyList [i].Type != null)
- compiler.CheckType (BodyList [i].Expression, BodyList [i].Type);
- }
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Boolean; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter.Context);
- }
- public override bool EvaluateAsBoolean (XPathSequence iter)
- {
- return EvaluateQuantification (iter, BodyList.GetEnumerator ());
- }
- private bool EvaluateQuantification (XPathSequence iter, IEnumerator bodies)
- {
- if (bodies.MoveNext ()) {
- QuantifiedExprBody qb = bodies.Current as QuantifiedExprBody;
- XPathSequence seq = qb.Expression.Evaluate (iter);
- bool passed = false;
- foreach (XPathItem item in seq) {
- passed = true;
- // FIXME: consider qb.Type
- try {
- iter.Context.PushVariable (qb.VarName, item);
- if (EvaluateQuantification (iter, bodies)) {
- if (!Every)
- return true;
- }
- else if (Every)
- return false;
- } finally {
- iter.Context.PopVariable ();
- }
- }
- return passed;
- }
- return Satisfies.EvaluateAsBoolean (iter);
- }
- #endregion
- }
- internal class QuantifiedExprBodyList : CollectionBase
- {
- public QuantifiedExprBodyList ()
- {
- }
- public void Add (QuantifiedExprBody body)
- {
- List.Add (body);
- }
- public void Insert (int pos, QuantifiedExprBody body)
- {
- List.Insert (pos, body);
- }
- public QuantifiedExprBody this [int i] {
- get { return (QuantifiedExprBody) List [i]; }
- }
- }
- internal class QuantifiedExprBody
- {
- private XmlQualifiedName varName;
- private SequenceType type;
- private ExprSingle expr;
- public QuantifiedExprBody (XmlQualifiedName varName,
- SequenceType type, ExprSingle expr)
- {
- this.varName = varName;
- this.type = type ;
- this.expr = expr;
- }
- public XmlQualifiedName VarName {
- get { return varName; }
- }
- public SequenceType Type {
- get { return type; }
- }
- public ExprSingle Expression {
- get { return expr; }
- set { expr = value; }
- }
- }
- // TypeswitchExpr
- internal class TypeswitchExpr : ExprSingle
- {
- ExprSequence switchExpr;
- CaseClauseList caseList;
- XmlQualifiedName defaultVarName;
- ExprSingle defaultReturn;
- public TypeswitchExpr (ExprSequence switchExpr, CaseClauseList caseList, XmlQualifiedName defaultVarName, ExprSingle defaultReturn)
- {
- this.switchExpr = switchExpr;
- this.caseList = caseList;
- this.defaultVarName = defaultVarName;
- this.defaultReturn = defaultReturn;
- }
- public ExprSequence SwitchExpr {
- get { return switchExpr; }
- }
- public CaseClauseList Cases {
- get { return caseList; }
- }
- public XmlQualifiedName DefaultVarName {
- get { return defaultVarName; }
- }
- public ExprSingle DefaultReturn {
- get { return defaultReturn; }
- set { defaultReturn = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- switchExpr.CheckReference (compiler);
- foreach (CaseClause cc in caseList) {
- compiler.CheckSchemaType (cc.Type);
- cc.Expr.CheckReference (compiler);
- }
- defaultReturn.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- for (int i = 0; i < SwitchExpr.Count; i++)
- SwitchExpr [i] = SwitchExpr [i].Compile (compiler);
- foreach (CaseClause cc in Cases)
- cc.Expr = cc.Expr.Compile (compiler);
- DefaultReturn = DefaultReturn.Compile (compiler);
- return this;
- }
- // FIXME: it can be optimized by checking all case clauses.
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- // FIXME: should move to iterator?
- XPathSequence cond = new ExprSequenceIterator (iter, SwitchExpr);
- XPathSequence ret = null;
- foreach (CaseClause ccc in Cases) {
- if (ccc.Type.Matches (cond)) {
- if (ccc.VarName != XmlQualifiedName.Empty)
- iter.Context.PushVariable (ccc.VarName, cond);
- ret = ccc.Expr.Evaluate (iter);
- // FIXME: The design should make sure that in-scope variables are held on actual iteration.
- if (ccc.VarName != XmlQualifiedName.Empty)
- iter.Context.PopVariable ();
- return ret;
- }
- }
- if (DefaultVarName != XmlQualifiedName.Empty)
- iter.Context.PushVariable (DefaultVarName, cond);
- ret = DefaultReturn.Evaluate (iter);
- if (DefaultVarName != XmlQualifiedName.Empty)
- iter.Context.PopVariable ();
- return ret;
- }
- #endregion
- }
- internal class CaseClauseList : CollectionBase
- {
- public void Insert (int pos, CaseClause cc)
- {
- List.Insert (pos, cc);
- }
- public void Add (CaseClause cc)
- {
- List.Add (cc);
- }
- public CaseClause this [int i] {
- get { return (CaseClause) List [i]; }
- }
- }
- internal class CaseClause
- {
- public CaseClause (SequenceType type, ExprSingle expr, XmlQualifiedName varName)
- {
- this.type = type;
- this.expr = expr;
- this.varName = varName;
- }
- SequenceType type;
- ExprSingle expr;
- XmlQualifiedName varName;
- public SequenceType Type {
- get { return type; }
- }
- public ExprSingle Expr {
- get { return expr; }
- set { expr = value; }
- }
- public XmlQualifiedName VarName {
- get { return varName; }
- set { varName = value; }
- }
- }
- // IfExpr
- internal class IfExpr : ExprSingle
- {
- public IfExpr (ExprSequence condition, ExprSingle trueExpr, ExprSingle falseExpr)
- {
- this.condition = new ParenthesizedExpr (condition);
- this.trueExpr = trueExpr;
- this.falseExpr = falseExpr;
- }
- ExprSingle condition;
- ExprSingle trueExpr;
- ExprSingle falseExpr;
- public ExprSingle Condition {
- get { return condition; }
- set { condition = value; }
- }
- public ExprSingle TrueExpr {
- get { return trueExpr; }
- set { trueExpr = value; }
- }
- public ExprSingle FalseExpr {
- get { return falseExpr; }
- set { falseExpr = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- condition.CheckReference (compiler);
- trueExpr.CheckReference (compiler);
- falseExpr.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- SequenceType computedReturnType;
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- condition = condition.Compile (compiler);
- // FIXME: check if condition is constant, and returns trueExpr or falseExpr
- TrueExpr = TrueExpr.Compile (compiler);
- FalseExpr = FalseExpr.Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get {
- if (Context == null)
- return SequenceType.AnyType;
- if (computedReturnType == null)
- computedReturnType = SequenceType.ComputeCommonBase (TrueExpr.StaticType, FalseExpr.StaticType);
- return computedReturnType;
- }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- if (condition.EvaluateAsBoolean (iter))
- return TrueExpr.Evaluate (iter);
- return FalseExpr.Evaluate (iter);
- }
- #endregion
- }
- // logical expr
- internal abstract class BinaryOperationExpr : ExprSingle
- {
- protected BinaryOperationExpr (ExprSingle left, ExprSingle right)
- {
- this.left = left;
- this.right = right;
- }
- ExprSingle left, right;
-
- public ExprSingle Left {
- get { return left; }
- set { left = value; }
- }
- public ExprSingle Right{
- get { return right; }
- set { right = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- left.CheckReference (compiler);
- right.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Left = Left.Compile (compiler);
- Right = Right.Compile (compiler);
- return this;
- }
- #endregion
- }
- internal class OrExpr : BinaryOperationExpr
- {
- public OrExpr (ExprSingle left, ExprSingle right)
- : base (left, right)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- base.CompileCore (compiler);
- // FIXME: check constant value and return true or false
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Boolean; }
- }
- public override bool EvaluateAsBoolean (XPathSequence iter)
- {
- return Left.EvaluateAsBoolean (iter) || Right.EvaluateAsBoolean (iter);
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (EvaluateAsBoolean (iter) ?AtomicTrue : AtomicFalse, iter.Context);
- }
- /*
- - compiler -
- return leftExprBool (context) || rightExprBool (context);
- */
- #endregion
- }
- internal class AndExpr : BinaryOperationExpr
- {
- public AndExpr (ExprSingle left, ExprSingle right)
- : base (left, right)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- base.CompileCore (compiler);
- // FIXME: check constant value and return true or false
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Boolean; }
- }
- public override bool EvaluateAsBoolean (XPathSequence iter)
- {
- return Left.EvaluateAsBoolean (iter) && Right.EvaluateAsBoolean (iter);
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter.Context);
- }
- /*
- - compiler -
- return leftExprBool (context) && rightExprBool (context);
- */
- #endregion
- }
- // TypeOperation expr
- internal abstract class TypeOperationExpr : ExprSingle
- {
- protected TypeOperationExpr (ExprSingle expr, SequenceType type)
- {
- this.expr = expr;
- this.type = type;
- }
- ExprSingle expr;
- SequenceType type;
- public ExprSingle Expr {
- get { return expr; }
- set { expr = value; }
- }
- public SequenceType TargetType {
- get { return type; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- compiler.CheckSchemaType (type);
- }
- }
- internal abstract class AtomicTypeOperationExpr : ExprSingle
- {
- protected AtomicTypeOperationExpr (ExprSingle expr, XmlTypeCode type, bool optional)
- {
- this.expr = expr;
- this.targetType = SequenceType.Create (type, optional ? Occurence.Optional : Occurence.One);
- }
- ExprSingle expr;
- SequenceType targetType;
- internal ExprSingle Expr {
- get { return expr; }
- set { expr = value; }
- }
- /*
- public XmlTypeCode TypeCode {
- get { return typeCode; }
- }
- public bool Optional {
- get { return optional; }
- }
- */
- internal SequenceType TargetType {
- get { return targetType; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- }
- }
- internal class InstanceOfExpr : TypeOperationExpr
- {
- public InstanceOfExpr (ExprSingle expr, SequenceType type)
- : base (expr, type)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Expr = Expr.Compile (compiler);
- // FIXME: check return type and if it never matches then return false
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Boolean; }
- }
- public override bool EvaluateAsBoolean (XPathSequence iter)
- {
- bool occured = false;
- bool onlyOnce = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.Optional);
- bool required = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.OneOrMore);
- foreach (XPathItem item in iter) {
- if (occured && onlyOnce)
- return false;
- if (!TargetType.IsInstance (item))
- return false;
- }
- return occured || !required;
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter.Context);
- }
- #endregion
- }
- internal class TreatExpr : TypeOperationExpr
- {
- public TreatExpr (ExprSingle expr, SequenceType type)
- : base (expr, type)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Expr = Expr.Compile (compiler);
- // FIXME: check return type and if it never matches then return false
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- if (TargetType.CanConvert (iter))
- return iter;
- else
- throw new XmlQueryException (String.Format ("Cannot treat as {1}", TargetType));
- }
- #endregion
- }
- internal class CastableExpr : AtomicTypeOperationExpr
- {
- public CastableExpr (ExprSingle expr, XmlTypeCode atomicType, bool optional)
- : base (expr, atomicType, optional)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Expr = Expr.Compile (compiler);
- // FIXME: check return type and if it never matches then return boolean
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Boolean; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (new XPathAtomicValue (EvaluateAsBoolean (iter), XmlSchemaSimpleType.XsBoolean), iter.Context);
- }
- public override bool EvaluateAsBoolean (XPathSequence iter)
- {
- bool occured = false;
- bool onlyOnce = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.Optional);
- bool required = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.OneOrMore);
- foreach (XPathItem item in iter) {
- if (occured && onlyOnce)
- return false;
- if (!TargetType.CanConvert (item))
- return false;
- }
- return occured || !required;
- }
- #endregion
- }
- internal class CastExpr : AtomicTypeOperationExpr
- {
- public CastExpr (ExprSingle expr, XmlTypeCode atomicType, bool optional)
- : base (expr, atomicType, optional)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Expr = Expr.Compile (compiler);
- // FIXME: check return type and if it never matches then return boolean
- return this;
- }
- public override SequenceType StaticType {
- get { return TargetType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- if (TargetType.CanConvert (iter))
- return new ConvertingIterator (iter, TargetType);
- else
- throw new XmlQueryException (String.Format ("Cannot cast as {1}", TargetType));
- }
- #endregion
- }
- // ComparisonExpr
- internal class ComparisonExpr : BinaryOperationExpr
- {
- public ComparisonExpr (ExprSingle left, ExprSingle right, ComparisonOperator oper)
- : base (left, right)
- {
- this.oper = oper;
- }
- ComparisonOperator oper;
- public ComparisonOperator Operation {
- get { return oper; }
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Left = Left.Compile (compiler);
- Right = Right.Compile (compiler);
- // FIXME: check return type and if it never matches then return boolean
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Boolean; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- bool isEmpty;
- bool result = EvaluateAsBoolean (iter, out isEmpty);
- if (isEmpty)
- return new XPathEmptySequence (iter.Context);
- return new SingleItemIterator (result ? AtomicTrue : AtomicFalse, iter.Context);
- }
- public override bool EvaluateAsBoolean (XPathSequence iter)
- {
- bool isEmpty;
- return EvaluateAsBoolean (iter, out isEmpty);
- }
- private bool EvaluateAsBoolean (XPathSequence iter, out bool isEmpty)
- {
- XPathSequence lseq, rseq;
- isEmpty = false;
- switch (Operation) {
- // FIXME: it is curious but currently gmcs requires full typename.
- case Mono.Xml.XPath2.ComparisonOperator.ValueEQ:
- case Mono.Xml.XPath2.ComparisonOperator.ValueNE:
- case Mono.Xml.XPath2.ComparisonOperator.ValueLT:
- case Mono.Xml.XPath2.ComparisonOperator.ValueLE:
- case Mono.Xml.XPath2.ComparisonOperator.ValueGT:
- case Mono.Xml.XPath2.ComparisonOperator.ValueGE:
- XPathItem itemVL = ExamineOneItem (Left.Evaluate (iter));
- XPathItem itemVR = ExamineOneItem (Right.Evaluate (iter));
- if (itemVL == null || itemVR == null) {
- isEmpty = true;
- return false;
- }
- return CompareAtomic (itemVL, itemVR);
- case Mono.Xml.XPath2.ComparisonOperator.GeneralEQ:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralNE:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralLT:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralLE:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralGT:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralGE:
- lseq = Left.Evaluate (iter);
- rseq = Right.Evaluate (iter);
- foreach (XPathItem itemGL in lseq) {
- foreach (XPathItem itemGR in rseq.Clone ()) {
- if (CompareAtomic (itemGL, itemGR))
- return true;
- }
- }
- return false;
- case Mono.Xml.XPath2.ComparisonOperator.NodeIs:
- case Mono.Xml.XPath2.ComparisonOperator.NodeFWD:
- case Mono.Xml.XPath2.ComparisonOperator.NodeBWD:
- XPathNavigator lnav = ExamineOneNode (Left.Evaluate (iter));
- XPathNavigator rnav = ExamineOneNode (Right.Evaluate (iter));
- if (lnav == null || rnav == null) {
- isEmpty = true;
- return false;
- }
- switch (Operation) {
- case Mono.Xml.XPath2.ComparisonOperator.NodeIs:
- return lnav.IsSamePosition (rnav);
- case Mono.Xml.XPath2.ComparisonOperator.NodeFWD:
- return lnav.ComparePosition (rnav) == XmlNodeOrder.Before;
- case Mono.Xml.XPath2.ComparisonOperator.NodeBWD:
- return lnav.ComparePosition (rnav) == XmlNodeOrder.After;
- }
- break;
- }
- throw new SystemException ("XQuery internal error: should not happen.");
- }
- // returns null if sequence was empty
- private XPathItem ExamineOneItem (XPathSequence seq)
- {
- if (!seq.MoveNext ())
- return null;
- XPathItem item = seq.Current;
- if (seq.MoveNext ())
- throw new XmlQueryException ("Operand of value comparison expression must be evaluated as a sequence that contains exactly one item.");
- return item;
- }
- // returns null if sequence was empty
- private XPathNavigator ExamineOneNode (XPathSequence seq)
- {
- if (!seq.MoveNext ())
- return null;
- XPathNavigator nav = seq.Current as XPathNavigator;
- if (nav == null || seq.MoveNext ())
- throw new XmlQueryException ("Operand of node comparison expression must be evaluated as a sequence that contains exactly one node.");
- return nav;
- }
- private bool CompareAtomic (XPathItem itemL, XPathItem itemR)
- {
- XmlSchemaSimpleType ua = XmlSchemaSimpleType.XdtUntypedAtomic;
- XmlSchemaSimpleType str = XmlSchemaSimpleType.XsString;
- // FIXME: XPathNavigator might be complex content.
- bool uaL = itemL.XmlType == null || itemL.XmlType == ua;
- bool uaR = itemR.XmlType == null || itemR.XmlType == ua;
- bool bothUA = uaL && uaR;
- XPathAtomicValue avL =
- (uaL) ?
- bothUA ? new XPathAtomicValue (itemL.Value, str) :
- new XPathAtomicValue (itemL.Value, itemR.XmlType) :
- Atomize (itemL);
- XPathAtomicValue avR =
- uaR ?
- bothUA ? new XPathAtomicValue (itemR.Value, str) :
- new XPathAtomicValue (itemR.Value, itemL.XmlType) :
- Atomize (itemR);
- switch (Operation) {
- // FIXME: it is curious but currently gmcs requires full typename.
- case Mono.Xml.XPath2.ComparisonOperator.ValueEQ:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralEQ:
- return XQueryComparisonOperator.ValueEQ (avL, avR);
- case Mono.Xml.XPath2.ComparisonOperator.ValueNE:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralNE:
- return XQueryComparisonOperator.ValueNE (avL, avR);
- case Mono.Xml.XPath2.ComparisonOperator.ValueLT:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralLT:
- return XQueryComparisonOperator.ValueLT (avL, avR);
- case Mono.Xml.XPath2.ComparisonOperator.ValueLE:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralLE:
- return XQueryComparisonOperator.ValueLE (avL, avR);
- case Mono.Xml.XPath2.ComparisonOperator.ValueGT:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralGT:
- return XQueryComparisonOperator.ValueGT (avL, avR);
- case Mono.Xml.XPath2.ComparisonOperator.ValueGE:
- case Mono.Xml.XPath2.ComparisonOperator.GeneralGE:
- return XQueryComparisonOperator.ValueGE (avL, avR);
- }
- return false; // should not happen
- }
- #endregion
- }
- public enum ComparisonOperator {
- ValueEQ,
- ValueNE,
- ValueLT,
- ValueLE,
- ValueGT,
- ValueGE,
- GeneralEQ,
- GeneralNE,
- GeneralLT,
- GeneralLE,
- GeneralGT,
- GeneralGE,
- NodeIs,
- NodeFWD,
- NodeBWD
- }
- // Range
- internal class RangeExpr : BinaryOperationExpr
- {
- public RangeExpr (ExprSingle left, ExprSingle right)
- : base (left, right)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Left = Left.Compile (compiler);
- Right = Right.Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.IntegerList; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- int start = Left.EvaluateAsInt (iter);
- int end = Right.EvaluateAsInt (iter);
- return new IntegerRangeIterator (iter.Context, start, end);
- }
- public override void Serialize (XPathSequence iter)
- {
- int start = Left.EvaluateAsInt (iter);
- int end = Right.EvaluateAsInt (iter);
- for (int i = start; i <= end; i++) {
- iter.Context.Writer.WriteValue (i);
- if (i < end)
- iter.Context.Writer.WriteWhitespace (" ");
- }
- }
- #endregion
- }
- // arithmetic operation expr
- public enum ArithmeticOperator {
- Add,
- Sub,
- Mul,
- Div,
- IDiv,
- IMod
- }
- internal class ArithmeticOperationExpr : BinaryOperationExpr
- {
- public ArithmeticOperationExpr (ExprSingle left, ExprSingle right, ArithmeticOperator oper)
- : base (left, right)
- {
- this.oper = oper;
- }
- ArithmeticOperator oper;
- public ArithmeticOperator Operation {
- get { return oper; }
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Left = Left.Compile (compiler);
- Right = Right.Compile (compiler);
- return this;
- }
- // FIXME: It can be optimized by comparing l/r value types.
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- XPathSequence lseq = Left.Evaluate (iter);
- if (!lseq.MoveNext ())
- return new XPathEmptySequence (iter.Context);
- XPathSequence rseq = Right.Evaluate (iter);
- if (!rseq.MoveNext ())
- return new XPathEmptySequence (iter.Context);
- XPathAtomicValue lvalue = Atomize (lseq.Current);
- XPathAtomicValue rvalue = Atomize (rseq.Current);
- if (lseq.MoveNext ())
- throw new XmlQueryException ("XP0006: Left operand resulted in an sequence that contains more than one item.");
- if (rseq.MoveNext ())
- throw new XmlQueryException ("XP0006: Left operand resulted in an sequence that contains more than one item.");
- // FIXME: handle "untypedAtomic to xs:double" casting
- return new SingleItemIterator (Compute (lvalue, rvalue), iter.Context);
- }
- private XPathAtomicValue Compute (XPathAtomicValue lvalue, XPathAtomicValue rvalue)
- {
- switch (Operation) {
- case ArithmeticOperator.Add:
- return XQueryArithmeticOperator.Add (lvalue, rvalue);
- case ArithmeticOperator.Sub:
- return XQueryArithmeticOperator.Subtract (lvalue, rvalue);
- case ArithmeticOperator.Mul:
- return XQueryArithmeticOperator.Multiply (lvalue, rvalue);
- case ArithmeticOperator.Div:
- return XQueryArithmeticOperator.Divide (lvalue, rvalue);
- case ArithmeticOperator.IDiv:
- return XQueryArithmeticOperator.IntDivide (lvalue, rvalue);
- case ArithmeticOperator.IMod:
- return XQueryArithmeticOperator.Remainder (lvalue, rvalue);
- default:
- throw new SystemException ("XQuery internal error: should not happen.");
- }
- }
- #endregion
- }
- internal class MinusExpr : ExprSingle
- {
- public MinusExpr (ExprSingle expr)
- {
- this.expr = expr;
- }
- ExprSingle expr;
- public ExprSingle Expr {
- get { return expr; }
- set { expr = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- return new ArithmeticOperationExpr (new DecimalLiteralExpr (-1), Expr, ArithmeticOperator.Mul).Compile (compiler);
- }
- public override SequenceType StaticType {
- get { return Expr.StaticType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- throw new SystemException ("XQuery internal error: should not happen.");
- }
- #endregion
- }
- // aggregation expr
- public enum AggregationType {
- Union,
- Intersect,
- Except
- }
- internal class GroupExpr : BinaryOperationExpr
- {
- public GroupExpr (ExprSingle left, ExprSingle right, AggregationType aggrType)
- : base (left, right)
- {
- this.aggrType = aggrType;
- }
- AggregationType aggrType;
- public AggregationType AggregationType {
- get { return aggrType; }
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Left = Left.Compile (compiler);
- Right = Right.Compile (compiler);
- return this;
- }
- // FIXME: It can be optimized by comparing l/r value types.
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- // only applicable against node-sets
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new GroupIterator (iter, this);
- }
- #endregion
- }
- // validate expr
- internal class ValidateExpr : ExprSingle
- {
- XmlSchemaContentProcessing schemaMode;
- ExprSequence expr;
- public ValidateExpr (XmlSchemaContentProcessing schemaMode, ExprSequence expr)
- {
- this.schemaMode = schemaMode;
- this.expr = expr;
- }
- public ExprSequence Expr {
- get { return expr; }
- }
- public XmlSchemaContentProcessing SchemaMode {
- get { return schemaMode; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- for (int i = 0; i < expr.Count; i++)
- expr [i] = expr [i].Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- // TBD (see 3.13).
- throw new NotImplementedException ();
- }
- #endregion
- }
- // Path expr
- internal abstract class PathExpr : ExprSingle
- {
- }
- // '/'
- internal class PathRootExpr : PathExpr
- {
- public PathRootExpr ()
- {
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Document; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- XPathNavigator nav = iter.Context.CurrentItem as XPathNavigator;
- if (nav == null)
- throw new XmlQueryException ("Context item is not a node when evaluating expression '/'.");
- nav = nav.Clone ();
- nav.MoveToRoot ();
- return new SingleItemIterator (nav, iter.Context);
- }
- #endregion
- }
- internal abstract class PathStepExpr : PathExpr
- {
- ExprSingle first;
- ExprSingle next;
- public PathStepExpr (ExprSingle first, ExprSingle next)
- {
- this.first = first;
- this.next = next;
- }
- public ExprSingle First {
- get { return first; }
- set { first = value; }
- }
- public ExprSingle Next {
- get { return next; }
- set { next = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- first.CheckReference (compiler);
- next.CheckReference (compiler);
- }
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- first = first.Compile (compiler);
- next = next.Compile (compiler);
- return this;
- }
- }
- // 'foo/bar'
- internal class PathSlashExpr : PathStepExpr
- {
- public PathSlashExpr (ExprSingle first, ExprSingle next)
- : base (first, next)
- {
- }
- #region CompileAndEvaluate
- // FIXME: It can be optimized by comparing l/r value types.
- public override SequenceType StaticType {
- get { return SequenceType.Node; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new PathStepIterator (First.Evaluate (iter), this);
- }
- #endregion
- }
- // 'foo//bar'
- internal class PathSlash2Expr : PathStepExpr
- {
- public PathSlash2Expr (ExprSingle first, ExprSingle next)
- : base (first, next)
- {
- }
- #region CompileAndEvaluate
- // FIXME: It can be optimized by comparing l/r value types.
- public override SequenceType StaticType {
- get { return SequenceType.Node; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- XPathSequence seq = First.Evaluate (iter);
- if (!seq.MoveNext ())
- return new XPathEmptySequence (iter.Context);
- return new PathStepIterator (
- new DescendantOrSelfIterator (seq.Current as XPathNavigator, seq.Context), this);
- }
- #endregion
- }
- internal class AxisStepExpr : PathExpr
- {
- public AxisStepExpr (XPathAxis axis, XPath2NodeTest test)
- {
- this.axis = axis;
- if (test == null)
- nameTest = XmlQualifiedName.Empty;
- else {
- if (test.NameTest != null)
- this.nameTest = test.NameTest;
- else
- this.kindTest = test.KindTest;
- }
- }
- XPathAxis axis;
- XmlQualifiedName nameTest;
- KindTest kindTest;
- public XPathAxis Axis {
- get { return axis; }
- }
- public XmlQualifiedName NameTest {
- get { return nameTest; }
- set { nameTest = value; }
- }
- public KindTest KindTest {
- get { return kindTest; }
- set { kindTest = value; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (KindTest != null)
- KindTest.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (KindTest != null)
- KindTest.Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get {
- switch (Axis.AxisType) {
- case XPathAxisType.Attribute:
- return SequenceType.Attribute;
- case XPathAxisType.Namespace:
- return SequenceType.Namespace;
- }
- // FIXME: we can more filtering by KindTest
- return SequenceType.Node;
- }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- XQueryContext ctx = iter.Context;
- if (iter.Position == 0) {
- iter = iter.Clone ();
- if (!iter.MoveNext ())
- return new XPathEmptySequence (iter.Context);
- }
- XPathNavigator nav = iter.Current as XPathNavigator;
- if (nav == null)
- throw new XmlQueryException ("Node set is expected.");
- NodeIterator argIter = null;
- switch (Axis.AxisType) {
- case XPathAxisType.Child:
- argIter = new ChildIterator (nav, ctx); break;
- case XPathAxisType.Descendant:
- argIter = new DescendantIterator (nav, ctx); break;
- case XPathAxisType.Attribute:
- argIter = new AttributeIterator (nav, ctx); break;
- case XPathAxisType.Self:
- argIter = new SelfIterator (nav, ctx); break;
- case XPathAxisType.DescendantOrSelf:
- argIter = new DescendantOrSelfIterator (nav, ctx); break;
- case XPathAxisType.FollowingSibling:
- argIter = new FollowingSiblingIterator (nav, ctx); break;
- case XPathAxisType.Following:
- argIter = new FollowingIterator (nav, ctx); break;
- case XPathAxisType.Parent:
- argIter = new ParentIterator (nav, ctx); break;
- case XPathAxisType.Ancestor:
- argIter = new AncestorIterator (nav, ctx); break;
- case XPathAxisType.PrecedingSibling:
- argIter = new PrecedingSiblingIterator (nav, ctx); break;
- case XPathAxisType.Preceding:
- argIter = new PrecedingIterator (nav, ctx); break;
- case XPathAxisType.AncestorOrSelf:
- argIter = new AncestorOrSelfIterator (nav, ctx); break;
- case XPathAxisType.Namespace: // only applicable under XPath 2.0: not XQuery 1.0
- argIter = new NamespaceIterator (nav, ctx); break;
- }
- return new AxisIterator (argIter, this);
- }
- internal bool Matches (XPathNavigator nav)
- {
- if (nameTest != null)
- return nameTest == XmlQualifiedName.Empty ||
- ((nameTest.Name == nav.LocalName || nameTest.Name == "*") &&
- (nameTest.Namespace == nav.NamespaceURI || nameTest.Namespace == "*"));
- else
- return kindTest.Matches (nav);
- }
- #endregion
- }
- internal class FilterStepExpr : PathExpr
- {
- public FilterStepExpr (ExprSingle expr, ExprSequence predicate)
- {
- this.expr = expr;
- this.predicate = predicate;
- }
- ExprSingle expr;
- ExprSequence predicate;
- public ExprSingle Expr {
- get { return expr; }
- set { expr = value; }
- }
- public ExprSequence Predicate {
- get { return predicate; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- predicate.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- Expr = Expr.Compile (compiler);
- for (int i = 0; i < predicate.Count; i++)
- predicate [i] = predicate [i].Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get { return Expr.StaticType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new FilteredIterator (iter, this);
- }
- #endregion
- }
- /*
- // predicates == exprsequence list == list of list of exprsingle
- internal class PredicateList : CollectionBase
- {
- public void Add (ExprSequence expr)
- {
- List.Add (expr);
- }
- public void Insert (int pos, ExprSequence expr)
- {
- List.Insert (pos, expr);
- }
- public ExprSequence this [int i] {
- get { return (ExprSequence) List [i]; }
- }
- }
- */
- internal class XPath2NodeTest
- {
- public XPath2NodeTest (XmlQualifiedName nameTest)
- {
- this.NameTest = nameTest;
- }
-
- public XPath2NodeTest (KindTest kindTest)
- {
- this.KindTest = kindTest;
- }
- public XmlQualifiedName NameTest;
- public KindTest KindTest;
- }
- internal class EnclosedExpr : ExprSingle
- {
- ExprSequence expr;
- public EnclosedExpr (ExprSequence expr)
- {
- this.expr = expr;
- }
- public ExprSequence Expr {
- get { return expr; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Expr.Count == 1)
- return Expr [0].Compile (compiler);
- for (int i = 0; i < Expr.Count; i++)
- Expr [i] = Expr [i].Compile (compiler);
- return this;
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new ExprSequenceIterator (iter, Expr);
- }
- #endregion
- }
- // PrimaryExpr
- internal abstract class PrimaryExpr : ExprSingle
- {
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- return this;
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (EvaluateAsAtomic (iter), iter.Context);
- }
- #endregion
- }
- internal class StringLiteralExpr : PrimaryExpr
- {
- string literal;
- public StringLiteralExpr (string literal)
- {
- this.literal = literal;
- }
- public string Literal {
- get { return literal; }
- }
- #region CompileAndEvaluate
- XmlSchemaSimpleType stringType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("string", XmlSchema.Namespace));
- public override SequenceType StaticType {
- get { return SequenceType.AtomicString; }
- }
- public override string EvaluateAsString (XPathSequence iter)
- {
- return Literal;
- }
- public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
- {
- return new XPathAtomicValue (Literal, stringType);
- }
- #endregion
- }
- internal class DecimalLiteralExpr : PrimaryExpr
- {
- decimal value;
- public DecimalLiteralExpr (decimal value)
- {
- this.value = value;
- }
- public decimal Value {
- get { return value; }
- }
- #region CompileAndEvaluate
- XmlSchemaSimpleType decimalType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("decimal", XmlSchema.Namespace));
- public override SequenceType StaticType {
- get { return SequenceType.Decimal; }
- }
- public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
- {
- return new XPathAtomicValue (Value, decimalType);
- }
- #endregion
- }
- internal class DoubleLiteralExpr : PrimaryExpr
- {
- double value;
- public DoubleLiteralExpr (double value)
- {
- this.value = value;
- }
- public double Value {
- get { return value; }
- }
- #region CompileAndEvaluate
- XmlSchemaSimpleType doubleType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("double", XmlSchema.Namespace));
- public override SequenceType StaticType {
- get { return SequenceType.Double; }
- }
- public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
- {
- return new XPathAtomicValue (Value, doubleType);
- }
- #endregion
- }
- internal class VariableReferenceExpr : PrimaryExpr
- {
- XmlQualifiedName varName;
- public VariableReferenceExpr (XmlQualifiedName varName)
- {
- this.varName = varName;
- }
- public XmlQualifiedName VariableName {
- get { return varName; }
- }
- // FIXME: variable name must be stacked in any area
- // whereever variables are defined.
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- compiler.CheckVariableName (varName);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- // FIXME: try to resolve static context variable and return the actual value expression
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- XPathSequence variable = iter.Context.ResolveVariable (VariableName);
- // FIXME: if Evaluate() accepts XPathSequence, then XPathSequence must be public class (to make IXPath2Variable public).
- return variable;
- }
- #endregion
- }
- internal class ParenthesizedExpr : PrimaryExpr
- {
- ExprSequence expr;
- public ParenthesizedExpr (ExprSequence expr)
- {
- if (expr == null)
- expr = new ExprSequence ();
- this.expr = expr;
- }
- ExprSequence Expr {
- get { return expr; }
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Expr.Count == 1)
- return Expr [0].Compile (compiler);
- for (int i = 0; i < Expr.Count; i++)
- Expr [i] = Expr [i].Compile (compiler);
- return this;
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- switch (Expr.Count) {
- case 0:
- return new XPathEmptySequence (iter.Context);
- case 1:
- return Expr [0].Evaluate (iter);
- default:
- return new ExprSequenceIterator (iter, Expr);
- }
- }
- #endregion
- }
- // "."
- internal class ContextItemExpr : PrimaryExpr
- {
- public ContextItemExpr ()
- {
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.AnyType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return new SingleItemIterator (iter.Context.CurrentItem, iter.Context);
- }
- #endregion
- }
- internal abstract class FunctionCallExprBase : PrimaryExpr
- {
- XmlQualifiedName name;
- ExprSequence args;
- public FunctionCallExprBase (XmlQualifiedName name, ExprSequence args)
- {
- if (args == null)
- throw new ArgumentNullException (String.Format ("Function argument expressions for {0} is null.", name));
- this.name = name;
- this.args = args;
- }
- public XmlQualifiedName Name {
- get { return name; }
- }
- public ExprSequence Args {
- get { return args; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- compiler.CheckFunctionName (name);
- }
- #region CompileAndEvaluate
- /*
- internal static DefaultFunctionCall Create (
- XmlQualifiedName name,
- ExprSingle [] args,
- XQueryStaticContext ctx)
- {
- switch (name.Namespace) {
- case XQueryFunction.Namespace:
- switch (name.Name) {
- case "node-name":
- return new FnNodeNameCall (ctx, args);
- case "nilled":
- return new FnNilledCall (ctx, args);
- case "string":
- return new FnStringCall (ctx, args);
- case "data":
- return new FnDataCall (ctx, args);
- case "base-uri":
- return new FnBaseUriCall (ctx, args);
- case "document-uri":
- return new FnDocumentUriCall (ctx, args);
- case "error":
- return new FnErrorCall (ctx, args);
- case "trace":
- return new FnTraceCall (ctx, args);
- case "abs":
- return new FnAbsCall (ctx, args);
- case "ceiling":
- return new FnCeilingCall (ctx, args);
- case "floor":
- return new FnFloorCall (ctx, args);
- case "round":
- return new FnRoundCall (ctx, args);
- case "round-half-to-even":
- return new FnRoundHalfToEvenCall (ctx, args);
- case "codepoints-to-string":
- return new FnCodepointsToStringCall (ctx, args);
- case "string-to-codepoints":
- return new FnStringCallToCodepointsCall (ctx, args);
- }
- goto default;
- case XmlSchema.XdtNamespace:
- case XmlSchema.Namespace:
- XmlSchemaType type = XmlSchemaType.GetBuiltInSimpleType (name);
- if (type != null)
- return new AtomicConstructorCall (ctx, SequenceType.Create (type, Occurence.One), args);
- type = XmlSchemaType.GetBuiltInComplexType (name);
- if (type == null)
- goto default;
- return null;
- default:
- XQueryFunction func = ctx.CompileContext.InEffectFunctions [name];
- if (func != null)
- return new CustomFunctionCallExpression (ctx, args, func);
- return null;
- }
- }
- */
- internal void CheckArguments (XQueryASTCompiler compiler)
- {
- if (args.Count < MinArgs || args.Count > MaxArgs)
- // FIXME: add more info
- throw new XmlQueryCompileException (String.Format ("{0} is invalid for the number of {1} function argument. MinArgs = {2}, MaxArgs = {3}.", args.Count, name, MinArgs, MaxArgs));
- }
- public abstract int MinArgs { get; }
- public abstract int MaxArgs { get; }
- #endregion
- }
- internal class FunctionCallExpr : FunctionCallExprBase
- {
- public FunctionCallExpr (XmlQualifiedName name, ExprSequence args)
- : base (name, args)
- {
- }
- XQueryFunction function;
- public XQueryFunction Function {
- get { return function; }
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- // resolve function
- function = compiler.ResolveFunction (Name);
- CheckArguments (compiler);
- for (int i = 0; i < Args.Count; i++)
- Args [i] = Args [i].Compile (compiler);
- return this;
- }
- public override int MinArgs {
- get { return function.MinArgs; }
- }
- public override int MaxArgs {
- get { return function.MaxArgs; }
- }
- public override SequenceType StaticType {
- get { return function.ReturnType; }
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return Function.Evaluate (iter, Args);
- }
- // FIXME: add all overrides that delegates to XQueryFunction
- #endregion
- }
- /*
- #region CompileAndEvaluate
- // It is instantiated per function call expression.
- // (e.g. the example below contains 4 FunctionCallExpression instances:
- // "replace(node-name (node-before(/*)), 'foo', node-name($var))"
- internal class CustomFunctionCallExpr : FunctionCallExprBase
- {
- public CustomFunctionCallExpr (ExprSequence args, XQueryFunction function)
- : base (function.Name, args)
- {
- this.function = function;
- }
- XQueryFunction function;
- public XQueryFunction Function {
- get { return function; }
- }
- public override int MinArgs {
- get { return function.MinArgs; }
- }
- public override int MaxArgs {
- get { return function.MaxArgs; }
- }
- public override SequenceType StaticType {
- get { return function.ReturnType; }
- }
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- CheckArguments (compiler);
- for (int i = 0; i < Args.Count; i++)
- Args [i] = Args [i].Compile (compiler);
- return this;
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return Function.Evaluate (iter, Args);
- }
- // FIXME: add all overrides that delegates to XQueryFunction
- }
- #endregion
- */
- // Ordered / Unordered
- internal class OrderSpecifiedExpr : ExprSingle
- {
- bool ordered;
- ExprSequence expr;
-
- public OrderSpecifiedExpr (ExprSequence expr, bool ordered)
- {
- this.ordered = ordered;
- this.expr = expr;
- }
- public ExprSequence Expr {
- get { return expr; }
- }
- public bool Ordered {
- get { return ordered; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- expr.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- public override SequenceType StaticType {
- // FIXME: could be optimized by checking all the expressions
- get { return SequenceType.AnyType; }
- }
- public override bool RequireSorting {
- get { return Ordered; }
- }
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- for (int i = 0; i < Expr.Count; i++)
- Expr [i] = Expr [i].Compile (compiler);
- return this;
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- throw new NotImplementedException ();
- }
- #endregion
- }
- }
- #endif
|