예제 #1
0
        /// <summary>
        /// 如果是ContextfreeGrammar的关键字,那么不需要产生其函数定义。(因为已经有固定的定义了)
        /// </summary>
        /// <returns></returns>
        protected virtual bool IsContextfreeGrammarKeyword()
        {
            SourceCodeCharType charType = this.charTypeList[0];

            // identifier
            if (charType == SourceCodeCharType.UnderLine)
            {
                return(true);
            }
            if (charType == SourceCodeCharType.Letter)
            {
                return(true);
            }
            // number
            if (charType == SourceCodeCharType.Number)
            {
                return(true);
            }
            // constString
            if (charType == SourceCodeCharType.DoubleQuotation)
            {
                return(true);
            }
            // char
            if (charType == SourceCodeCharType.Quotation)
            {
                return(true);
            }
            // null
            // null is not a node.

            return(false);
        }
        /// <summary>
        /// 从<code>context.NextLetterIndex</code>开始获取下一个<code>Token</code>
        /// </summary>
        /// <returns></returns>
        protected Token NextToken(AnalyzingContext context)
        {
            var result = new Token();

            result.Line              = context.CurrentLine;
            result.Column            = context.CurrentColumn;
            result.IndexOfSourceCode = context.NextLetterIndex;
            var count = context.SourceCode.Length;

            if (context.NextLetterIndex < 0 || context.NextLetterIndex >= count)
            {
                return(result);
            }
            var  gotToken = false;
            char ch       = context.CurrentChar();
            SourceCodeCharType charType = ch.GetCharType();

            gotToken = TryGetToken(context, result, charType);
            if (gotToken)
            {
                result.Length = context.NextLetterIndex - result.IndexOfSourceCode;
                return(result);
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
        private static bool IsIdentifier(this TreeNodeType node)
        {
            if (!node.IsLeave)
            {
                throw new ArgumentException();
            }

            {
                char item = node.Content[0];
                SourceCodeCharType charType = item.GetCharType();

                if (!(
                        charType == SourceCodeCharType.UnderLine ||
                        charType == SourceCodeCharType.Letter))
                {
                    return(false);
                }
            }
            for (int i = 1; i < node.Content.Length; i++)
            {
                char item = node.Content[i];
                SourceCodeCharType charType = item.GetCharType();

                if (!(
                        charType == SourceCodeCharType.UnderLine ||
                        charType == SourceCodeCharType.Number ||
                        charType == SourceCodeCharType.Letter))
                {
                    return(false);
                }
            }

            return(true);
        }
        protected override bool TryGetToken(
            AnalyzingContext context, Token result, SourceCodeCharType charType)
        {
            bool gotToken = false;

            switch (charType)
            {
            case SourceCodeCharType.Letter:
                gotToken = GetLetter(result, context);
                break;

            case SourceCodeCharType.UnderLine:
                gotToken = Getunderline(result, context);
                break;

            case SourceCodeCharType.Or:
                gotToken = GetOr(result, context);
                break;

            case SourceCodeCharType.LessThan:
                gotToken = GetLessThan(result, context);
                break;

            case SourceCodeCharType.GreaterThan:
                gotToken = GetGreaterThan(result, context);
                break;

            case SourceCodeCharType.DoubleQuotation:
                gotToken = GetDoubleQuotation(result, context);
                break;

            case SourceCodeCharType.Semicolon:
                gotToken = GetSemicolon(result, context);
                break;

            case SourceCodeCharType.Colon:
                gotToken = GetColon(result, context);
                break;

            case SourceCodeCharType.Divide:
                gotToken = GetDivide(result, context);
                break;

            case SourceCodeCharType.Space:
                gotToken = GetSpace(result, context);
                break;

            default:
                gotToken = GetUnknown(result, context);
                break;
            }

            return(gotToken);
        }
예제 #5
0
        private static string GetLexicalAnalyzerName(string grammarId)
        {
            SourceCodeCharType charType = grammarId[0].GetCharType();

            if (charType == SourceCodeCharType.Letter || charType == SourceCodeCharType.UnderLine)
            {
                return(string.Format("{0}LexicalAnalyzer",
                                     ConstString2IdentifierHelper.ConstString2Identifier(grammarId)));
            }
            else
            {
                return(string.Format("_{0}LexicalAnalyzer",
                                     ConstString2IdentifierHelper.ConstString2Identifier(grammarId)));
            }
        }
예제 #6
0
        private static string GetTreeNodeConstTypeName(string grammarId, SyntaxParserMapAlgorithm algorithm)
        {
            SourceCodeCharType charType = grammarId[0].GetCharType();

            if (charType == SourceCodeCharType.Letter || charType == SourceCodeCharType.UnderLine)
            {
                return(string.Format("{0}{1}TreeNodeType",
                                     ConstString2IdentifierHelper.ConstString2Identifier(grammarId),
                                     algorithm));
            }
            else
            {
                return(string.Format("_{0}{1}TreeNodeType",
                                     ConstString2IdentifierHelper.ConstString2Identifier(grammarId),
                                     algorithm));
            }
        }
예제 #7
0
        private static void CodeGetRegularToken(List <LexiState> result, TreeNodeType node)
        {
            string             content  = node.Content;
            SourceCodeCharType charType = content[0].GetCharType();
            bool exists = false;

            foreach (var state in result)
            {
                if (state.Contains(charType))
                {
                    state.GetTokenList.TryInsert(new CodeGetToken(node));
                    exists = true;
                    break;
                }
            }
            if (!exists)
            {
                var state = new LexiState();
                state.CharTypeList.Add(charType);
                state.GetTokenList.TryInsert(new CodeGetToken(node));
                result.Add(state);
            }
        }
        private static string CheckNode(TreeNodeType node, out bool innderError)
        {
            string content = node.Content;
            {
                // 长度为0(即"")
                if (string.IsNullOrEmpty(content))
                {
                    innderError = true;
                    return(string.Format("Empty node is not allowed in [{0}]", node.Dump()));
                }
            }
            {
                // 有空格的
                int count = content.Count(x => x.GetCharType() == SourceCodeCharType.Space);
                if (count > 0)
                {
                    innderError = true;
                    return(string.Format("Space is not allowed in [{0}]", node.Content));
                }
            }
            {
                // 数字开头的
                if ('0' <= content[0] && content[0] <= '9')
                {
                    innderError = true;
                    return(string.Format("Starting with number is not allowed in [{0}]", node.Content));
                }
            }
            SourceCodeCharType charType = content[0].GetCharType();

            {
                if (charType == SourceCodeCharType.Letter || charType == SourceCodeCharType.UnderLine)
                {
                    // 标识符内混入符号
                    foreach (var item in content)
                    {
                        SourceCodeCharType type = item.GetCharType();
                        if (type != SourceCodeCharType.Letter &&
                            type != SourceCodeCharType.UnderLine &&
                            type != SourceCodeCharType.Number)
                        {
                            innderError = true;
                            return(string.Format("Only letter, _ and number are allowed in an identifier for [{0}]", node.Content));
                        }
                    }
                }
                else
                {
                    // 符号内混入标识符
                    foreach (var item in content)
                    {
                        SourceCodeCharType type = item.GetCharType();
                        if (type == SourceCodeCharType.Letter ||
                            type == SourceCodeCharType.UnderLine ||
                            type == SourceCodeCharType.Number)
                        {
                            innderError = true;
                            return(string.Format("No letter, _ or number is allowed in an operator for [{0}]", node.Content));
                        }
                    }
                }
            }

            // 检查到此,没有问题
            innderError = false;
            return(string.Empty);
        }
예제 #9
0
 /// <summary>
 /// 从ptNextLetter开始获取下一个Token
 /// </summary>
 /// <returns></returns>
 protected abstract bool TryGetToken(AnalyzingContext context, Token result, SourceCodeCharType charType);
예제 #10
0
 internal bool Contains(SourceCodeCharType sourceCodeCharType)
 {
     return(this.charTypeList.Contains(sourceCodeCharType));
 }