?? xqparser.java
字號:
// Copyright (c) 2001, 2002, 2003, 2004, 2006 Per M.A. Bothner and Brainfood Inc.// This is free software; for terms and warranty disclaimer see ./COPYING.package gnu.xquery.lang;import gnu.kawa.lispexpr.*;import gnu.mapping.*;import gnu.lists.*;import gnu.text.*;import gnu.expr.*;import gnu.math.IntNum;import java.util.Vector;import java.util.Stack;import java.io.File;import gnu.kawa.xml.*;import gnu.xml.*;import gnu.bytecode.*;import gnu.kawa.reflect.OccurrenceType;import gnu.kawa.reflect.SingletonType;import gnu.kawa.functions.Convert;import gnu.xquery.util.NamedCollator;import gnu.xquery.util.CastableAs;import gnu.xquery.util.QNameUtils;import gnu.xquery.util.RelativeStep;import gnu.xquery.util.ValuesFilter;import kawa.standard.require;/** A class to read xquery forms. */public class XQParser extends Lexer{ int curToken; Object curValue; /** Normally null. * 'C' means parsing the type of a 'cast as' or 'castable as'. */ int parseContext; /** True if we've seen a VarDecl, FunctionDecl, or OptionDecl. */ boolean seenDeclaration; String libraryModuleNamespace; /** Value of getLineNumber() at start of current token. * Sometimes set otherwise, to report errors. */ int curLine; /** Value of getColumnNumber() at start of current token. * Sometimes set otherwise, to report errors. */ int curColumn; XQuery interpreter; int seenPosition; int seenLast; public static boolean warnOldVersion = true; public static boolean warnHidePreviousDeclaration = false; /** The internal name of the variable containing '.', the context node. */ static final Symbol DOT_VARNAME = Symbol.makeUninterned("$dot$"); /** The pseduo-function position() is mapped to a reference. */ static final Symbol POSITION_VARNAME = Symbol.makeUninterned("$position$"); /** The pseduo-function last() is mapped to a reference to this variable. */ static final Symbol LAST_VARNAME = Symbol.makeUninterned("$last$"); public static final gnu.kawa.reflect.InstanceOf instanceOf = new gnu.kawa.reflect.InstanceOf(XQuery.getInstance(), "instance"); public static final CastableAs castableAs = CastableAs.castableAs; public static final Convert treatAs = Convert.as; NameLookup lexical; NamedCollator defaultCollator = null; /** The default order for empty sequences. * Either <code>'L'</code> (for "least") or <code>'G'</code> (for "greatest"). */ char defaultEmptyOrder = 'L'; boolean emptyOrderDeclarationSeen; Path baseURI = null; boolean baseURIDeclarationSeen; public void setStaticBaseUri (String uri) { try { baseURI = fixupStaticBaseUri(URIPath.valueOf(uri)); } catch (Throwable ex) { if (ex instanceof WrappedException) ex = ((WrappedException) ex).getCause(); error('e', "invalid URI: "+ex.getMessage()); } } static Path fixupStaticBaseUri (Path path) { path = path.getAbsolute(); if (path instanceof FilePath) path = URIPath.valueOf(path.toURI()); return path; } public String getStaticBaseUri () { Path path = baseURI; if (path == null) { Environment env = Environment.getCurrent(); Object value = env.get(Symbol.make("", "base-uri"), null, null); if (value != null) { if (value instanceof Path) path = (Path) path; else path = URIPath.valueOf(value.toString()); } if (path == null) { LineBufferedReader port = getPort(); path = port.getPath(); if (path instanceof FilePath && (! path.exists() || port instanceof TtyInPort || port instanceof CharArrayInPort)) path = null; } if (path == null) path = Path.currentPath(); path = fixupStaticBaseUri(path); baseURI = path; } return path.toString(); } public String resolveAgainstBaseUri (String uri) { if (Path.uriSchemeSpecified(uri)) return uri; String base = getStaticBaseUri(); Path basePath = Path.valueOf(base); return basePath.resolve(uri).toString(); } boolean boundarySpacePreserve; boolean boundarySpaceDeclarationSeen; boolean orderingModeUnordered; boolean orderingModeSeen; /** True if we've seen a 'copy-namespaces' declaration'. */ boolean copyNamespacesDeclarationSeen; int copyNamespacesMode = XMLFilter.COPY_NAMESPACES_PRESERVE|XMLFilter.COPY_NAMESPACES_INHERIT; /** The static construction mode. True if "strip"; false if "preserve". */ boolean constructionModeStrip; /** True if a construction mode declaration has been seen. */ boolean constructionModeDeclarationSeen; public Namespace[] functionNamespacePath = XQuery.defaultFunctionNamespacePath; /** Stack of currently active for/let Declarations. */ Declaration[] flworDecls; /* Index in flworDecls of first Declaration in current FLWOR. */ int flworDeclsFirst; /* Total number of currently active for/let Declarations. */ int flworDeclsCount; int parseCount; int commentCount; /** An error message if comments are disallowed. Normally null. */ String errorIfComment; /** Skip whitespace. * Sets 'index' to the that of the next non-whitespace character, * and returns that. If there are no more non-space characters, * returns ' '. */ final int skipSpace() throws java.io.IOException, SyntaxException { return skipSpace(true); } final int skipSpace(boolean verticalToo) throws java.io.IOException, SyntaxException { for (;;) { int ch = read(); if (ch == '(') { if (! checkNext(':')) return '('; skipComment(); } else if (ch == '{') { ch = read(); if (ch != '-') { unread(ch); return '{'; } ch = read(); if (ch != '-') { unread(ch); unread('-'); return '{'; } skipOldComment(); } else if (verticalToo ? (ch < 0 || ! Character.isWhitespace((char) ch)) : (ch != ' ' && ch != '\t')) return ch; } } final void skipToSemicolon () throws java.io.IOException { for (;;) { int next = read(); if (next < 0 || next == ';') break; } } final void skipOldComment() throws java.io.IOException, SyntaxException { int seenDashes = 0; int startLine = getLineNumber() + 1; int startColumn = getColumnNumber() - 2; warnOldVersion("use (: :) instead of old-style comment {-- --}"); for (;;) { int ch = read(); if (ch == '-') seenDashes++; else if (ch == '}' && seenDashes >= 2) return; else if (ch < 0) { curLine = startLine; curColumn = startColumn; eofError("non-terminated comment starting here"); } else seenDashes = 0; } } final void skipComment() throws java.io.IOException, SyntaxException { commentCount++; int startLine = getLineNumber() + 1; int startColumn = getColumnNumber() - 1; if (errorIfComment != null) { curLine = startLine; curColumn = startColumn; error('e', errorIfComment); } int prev = 0; int commentNesting = 0; char saveReadState = pushNesting(':'); for (;;) { int ch = read(); if (ch == ':') { if (prev == '(') { commentNesting++; ch = 0; } } else if (ch == ')' && prev == ':') { if (commentNesting == 0) { popNesting(saveReadState); return; } --commentNesting; } else if (ch < 0) { curLine = startLine; curColumn = startColumn; eofError("non-terminated comment starting here"); } prev = ch; } } /** Do skipSpace followed by unread to find next non-space character. */ final int peekNonSpace(String message) throws java.io.IOException, SyntaxException { int ch = skipSpace(); if (ch < 0) eofError(message); unread(ch); return ch; } static final int EOF_TOKEN = -1; static final int EOL_TOKEN = '\n'; static final char INTEGER_TOKEN = '0'; static final char DECIMAL_TOKEN = '1'; static final char DOUBLE_TOKEN = '2'; static final int STRING_TOKEN = '"'; static final int SLASHSLASH_TOKEN = 'D'; static final int DOTDOT_TOKEN = '3'; static final int COLON_EQUAL_TOKEN = 'L'; // ":=" static final int COLON_COLON_TOKEN = 'X'; /** A non-qualified (simple) name (NCName). * The tokenBuffer contains the name (which does not contain a ':'). */ static final int NCNAME_TOKEN = 'A'; /** A non-qualified (simple) name (NCName) followed by a colon. * The colon is not followed by another NCNAME (in which it would * be a QNAME_TOKEN instead). * The tokenBuffer contains the name (which does not contain the ':'). */ static final int NCNAME_COLON_TOKEN = 'C'; /** A Qualified name (QName). * The tokenBuffer contains the full name, which contains one ':'. */ static final int QNAME_TOKEN = 'Q'; static final int ARROW_TOKEN = 'R'; /* FuncName including following '('). */ static final int FNAME_TOKEN = 'F'; static final int IMPORT_MODULE_TOKEN = 'I'; // <"import" "module"> static final int IMPORT_SCHEMA_TOKEN = 'T'; // <"import" "schema"> static final int MODULE_NAMESPACE_TOKEN = 'M'; // <"module" "namespace"> static final int DECLARE_NAMESPACE_TOKEN = 'N'; // <"declare" "namespace"> static final int DECLARE_BOUNDARY_SPACE_TOKEN = 'S'; // <"declare" "boundary-space"> static final int DEFAULT_ELEMENT_TOKEN = 'E'; // <"declare" "default" "element"> static final int DEFAULT_FUNCTION_TOKEN = 'O'; // <"declare" "default" "function"> static final int DEFAULT_COLLATION_TOKEN = 'G'; static final int DEFAULT_ORDER_TOKEN = 'H'; // <"declare" "default" "order"> static final int DECLARE_FUNCTION_TOKEN = 'P'; // <"declare" "function"> static final int DECLARE_VARIABLE_TOKEN = 'V'; // <"declare" "variable"> static final int DECLARE_BASE_URI_TOKEN = 'B'; // <"declare" "base-uri"> static final int DECLARE_ORDERING_TOKEN = 'U'; // <"declare" "ordering"> static final int DECLARE_CONSTRUCTION_TOKEN = 'K'; // <"declare" "construction"> static final int DECLARE_OPTION_TOKEN = 'o'; // <"declare" "option"> static final int DECLARE_COPY_NAMESPACES_TOKEN = 'L'; // <"declare" "copy-namespaces"> static final int DEFINE_QNAME_TOKEN = 'W'; // <"define" QName> - an error static final int XQUERY_VERSION_TOKEN = 'Y'; // <"xquery" "version"> /* 'Q': QName (intern'ed name is curValue) * 'R': NCName ':' '*' * OP_AXIS_FIRST: 'ancestor' followed by '::' * ... * OP_AXIS_FIRST+AXIS_SELF: 'self' followed by '::' */ static final int OP_AXIS_FIRST = 100; static final int COUNT_OP_AXIS = 13; static final int AXIS_ANCESTOR = 0; static final int AXIS_ANCESTOR_OR_SELF = 1; static final int AXIS_ATTRIBUTE = 2; static final int AXIS_CHILD = 3; static final int AXIS_DESCENDANT = 4; static final int AXIS_DESCENDANT_OR_SELF = 5; static final int AXIS_FOLLOWING = 6; static final int AXIS_FOLLOWING_SIBLING = 7; static final int AXIS_NAMESPACE = 8; static final int AXIS_PARENT = 9; static final int AXIS_PRECEDING = 10; static final int AXIS_PRECEDING_SIBLING = 11; static final int AXIS_SELF = 12; static final int OP_WHERE = 196; static final int PRAGMA_START_TOKEN = 197; // '{#' // Token types for binary operators. static final int OP_BASE = 400; static final int OP_OR = OP_BASE; // 'or' static final int OP_AND = OP_BASE + 1; // 'and' static final int OP_EQU = OP_BASE + 2; // ' =' static final int OP_NEQ = OP_BASE + 3; // '! =' static final int OP_LSS = OP_BASE + 4; // '<' static final int OP_GRT = OP_BASE + 5; // '>' static final int OP_LEQ = OP_BASE + 6; // '< =' static final int OP_GEQ = OP_BASE + 7; // '> =' static final int OP_IS = OP_BASE + 8; // 'is' static final int OP_ISNOT = OP_BASE + 9; // 'isnot' static final int OP_GRTGRT = OP_BASE + 10; // '>>' static final int OP_LSSLSS = OP_BASE + 11; // '<<' static final int OP_RANGE_TO = OP_BASE + 12; // 'to' static final int OP_ADD = OP_BASE + 13; // '+' static final int OP_SUB = OP_BASE + 14; // '-' static final int OP_MUL = OP_BASE + 15; // '*' static final int OP_DIV = OP_BASE + 16; // 'div' static final int OP_IDIV = OP_BASE + 17; // 'idiv' static final int OP_MOD = OP_BASE + 18; // 'mod' static final int OP_UNION = OP_BASE + 19; // 'union' static final int OP_INTERSECT = OP_BASE + 20; // 'intersect' static final int OP_EXCEPT = OP_BASE + 21; // 'except' static final int OP_INSTANCEOF = OP_BASE + 22; // 'instance' 'of' static final int OP_TREAT_AS = OP_BASE + 23; // 'treat' 'as' static final int OP_CASTABLE_AS= OP_BASE + 24; // 'castable' 'as' static final int OP_CAST_AS = OP_BASE + 25; // 'cast' 'as' static final int OP_EQ = OP_BASE + 26; // 'eq' static final int OP_NE = OP_BASE + 27; // 'ne' static final int OP_LT = OP_BASE + 28; // 'lt' static final int OP_LE = OP_BASE + 29; // 'le static final int OP_GT = OP_BASE + 30; // 'gt' static final int OP_GE = OP_BASE + 31; // 'ge' static final int OP_NODE = 230; // 'node' followed by '(' static final int OP_TEXT = 231; // 'text' followed by '(' static final int OP_COMMENT = 232; // 'comment' followed by '(' static final int OP_PI = 233; // 'processing-instruction' '(' static final int OP_DOCUMENT = 234; // 'document-node' '(' static final int OP_ELEMENT = 235; // 'element' '(' static final int OP_ATTRIBUTE = 236; // 'attribute' '(' static final int OP_ITEM = 237; // 'item' '(' static final int OP_EMPTY_SEQUENCE = 238; // 'empty-sequence' '(' static final int OP_SCHEMA_ATTRIBUTE = 239; // 'schema-attribute' '(' static final int OP_SCHEMA_ELEMENT = 240; // 'schema-element' '(' static final int IF_LPAREN_TOKEN = 241; // 'if' '(' static final int TYPESWITCH_LPAREN_TOKEN = 242; // 'typeswitch' '(' static final int FOR_DOLLAR_TOKEN = 243; // 'for' '$' static final int LET_DOLLAR_TOKEN = 244; // 'let' '$' static final int SOME_DOLLAR_TOKEN = 245; // 'some' '$' static final int EVERY_DOLLAR_TOKEN = 246; // 'every' '$' static final int CASE_DOLLAR_TOKEN = 247; // 'case' '$' static final int VALIDATE_LBRACE_TOKEN = 248; // 'validate' '{' static final int ORDERED_LBRACE_TOKEN = 249; // 'ordered' '{' static final int UNORDERED_LBRACE_TOKEN = 250; // 'unordered' '{'
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -