⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cs

📁 Grammatica是一个C#和Java的语法分析程序生成器(编译器的编译器)。它可以用LL(k)语法创建可读的和带有注释的源代码。它也支持创建一个运行时语法分析器
💻 CS
📖 第 1 页 / 共 2 页
字号:
        }        /**         * Returns the production pattern for the starting production.         *           * @return the start production pattern, or         *         null if no patterns have been added         */        internal ProductionPattern GetStartPattern() {            if (patterns.Count <= 0) {                return null;            } else {                return (ProductionPattern) patterns[0];            }        }            /**         * Returns the ordered set of production patterns.         *          * @return the ordered set of production patterns         */        internal ICollection GetPatterns() {            return patterns;        }        /**         * Handles the parser entering a production. This method calls the         * appropriate analyzer callback if the node is not hidden. Note         * that this method will not call any callback if an error          * requiring recovery has ocurred.         *          * @param node           the parse tree node         */        internal void EnterNode(Node node) {            if (!node.IsHidden() && errorRecovery < 0) {                try {                    analyzer.Enter(node);                } catch (ParseException e) {                    AddError(e, false);                }            }        }                /**         * Handles the parser leaving a production. This method calls the         * appropriate analyzer callback if the node is not hidden, and          * returns the result. Note that this method will not call any          * callback if an error requiring recovery has ocurred.         *          * @param node           the parse tree node         *          * @return the parse tree node, or         *         null if no parse tree should be created         */        internal Node ExitNode(Node node) {            if (!node.IsHidden() && errorRecovery < 0) {                try {                    return analyzer.Exit(node);                } catch (ParseException e) {                    AddError(e, false);                }            }            return node;        }            /**         * Handles the parser adding a child node to a production. This          * method calls the appropriate analyzer callback. Note that this          * method will not call any callback if an error requiring          * recovery has ocurred.         *          * @param node           the parent parse tree node         * @param child          the child parse tree node, or null         */        internal void AddNode(Production node, Node child) {            if (errorRecovery >= 0) {                // Do nothing            } else if (node.IsHidden()) {                node.AddChild(child);            } else if (child != null && child.IsHidden()) {                for (int i = 0; i < child.GetChildCount(); i++) {                    AddNode(node, child.GetChildAt(i));                }            } else {                try {                    analyzer.Child(node, child);                } catch (ParseException e) {                    AddError(e, false);                }            }        }            /**         * Reads and consumes the next token in the queue. If no token         * was available for consumation, a parse error will be         * thrown.         *          * @return the token consumed         *          * @throws ParseException if the input stream couldn't be read or         *             parsed correctly         */        internal Token NextToken() {            Token  token = PeekToken(0);                    if (token != null) {                tokens.RemoveAt(0);                return token;            } else {                throw new ParseException(                    ParseException.ErrorType.UNEXPECTED_EOF,                    null,                    tokenizer.GetCurrentLine(),                    tokenizer.GetCurrentColumn());            }        }            /**         * Reads and consumes the next token in the queue. If no token was         * available for consumation, a parse error will be thrown. A          * parse error will also be thrown if the token id didn't match          * the specified one.          *         * @param id             the expected token id         *           * @return the token consumed         *          * @throws ParseException if the input stream couldn't be parsed         *             correctly, or if the token wasn't expected         */        internal Token NextToken(int id) {            Token      token = NextToken();            ArrayList  list;                        if (token.GetId() == id) {                if (errorRecovery > 0) {                    errorRecovery--;                }                return token;            } else {                list = new ArrayList(1);                list.Add(tokenizer.GetPatternDescription(id));                throw new ParseException(                    ParseException.ErrorType.UNEXPECTED_TOKEN,                    token.ToShortString(),                    list,                    token.GetStartLine(),                    token.GetStartColumn());            }        }            /**         * Returns a token from the queue. This method is used to check          * coming tokens before they have been consumed. Any number of          * tokens forward can be checked.          *          * @param steps          the token queue number, zero (0) for first         *          * @return the token in the queue, or         *         null if no more tokens in the queue         */        internal Token PeekToken(int steps) {            Token  token;                while (steps >= tokens.Count) {                try {                    token = tokenizer.Next();                    if (token == null) {                        return null;                    } else {                        tokens.Add(token);                    }                } catch (ParseException e) {                    AddError(e, true);                }            }            return (Token) tokens[steps];        }            /**         * Returns a string representation of this parser. The string will         * contain all the production definitions and various additional          * information.         *          * @return a detailed string representation of this parser         */        public override string ToString() {            StringBuilder  buffer = new StringBuilder();                        for (int i = 0; i < patterns.Count; i++) {                buffer.Append(ToString((ProductionPattern) patterns[i]));                 buffer.Append("\n");            }            return buffer.ToString();        }            /**         * Returns a string representation of a production pattern.         *          * @param prod           the production pattern         *          * @return a detailed string representation of the pattern         */        private string ToString(ProductionPattern prod) {            StringBuilder  buffer = new StringBuilder();            StringBuilder  indent = new StringBuilder();            LookAheadSet   set;            int            i;                buffer.Append(prod.GetName());            buffer.Append(" (");            buffer.Append(prod.GetId());            buffer.Append(") ");            for (i = 0; i < buffer.Length; i++) {                indent.Append(" ");            }            buffer.Append("= ");            indent.Append("| ");            for (i = 0; i < prod.GetAlternativeCount(); i++) {                if (i > 0) {                    buffer.Append(indent);                }                buffer.Append(ToString(prod.GetAlternative(i)));                buffer.Append("\n");            }            for (i = 0; i < prod.GetAlternativeCount(); i++) {                set = prod.GetAlternative(i).GetLookAhead();                if (set.GetMaxLength() > 1) {                    buffer.Append("Using ");                    buffer.Append(set.GetMaxLength());                    buffer.Append(" token look-ahead for alternative ");                     buffer.Append(i + 1);                    buffer.Append(": ");                    buffer.Append(set.ToString(tokenizer));                    buffer.Append("\n");                }            }            return buffer.ToString();        }                /**         * Returns a string representation of a production pattern          * alternative.         *          * @param alt            the production pattern alternative         *          * @return a detailed string representation of the alternative         */        private string ToString(ProductionPatternAlternative alt) {            StringBuilder  buffer = new StringBuilder();                for (int i = 0; i < alt.GetElementCount(); i++) {                if (i > 0) {                    buffer.Append(" ");                }                buffer.Append(ToString(alt.GetElement(i)));            }            return buffer.ToString();        }            /**         * Returns a string representation of a production pattern          * element.         *          * @param elem           the production pattern element         *          * @return a detailed string representation of the element         */        private string ToString(ProductionPatternElement elem) {            StringBuilder  buffer = new StringBuilder();            int            min = elem.GetMinCount();            int            max = elem.GetMaxCount();                if (min == 0 && max == 1) {                buffer.Append("[");            }            if (elem.IsToken()) {                buffer.Append(GetTokenDescription(elem.GetId()));            } else {                buffer.Append(GetPattern(elem.GetId()).GetName());            }            if (min == 0 && max == 1) {                buffer.Append("]");            } else if (min == 0 && max == Int32.MaxValue) {                buffer.Append("*");            } else if (min == 1 && max == Int32.MaxValue) {                buffer.Append("+");            } else if (min != 1 || max != 1) {                buffer.Append("{");                buffer.Append(min);                buffer.Append(",");                buffer.Append(max);                buffer.Append("}");            }            return buffer.ToString();        }            /**         * Returns a token description for a specified token.         *          * @param token          the token to describe         *          * @return the token description         */        internal string GetTokenDescription(int token) {            if (tokenizer == null) {                return "";            } else {                return tokenizer.GetPatternDescription(token);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -