Exemplo n.º 1
0
        private void CreateRuleForANY(List <Pair> trace, SubRuleAny ANY, RuleDeclaration rdec)
        {
            foreach (string str in ANY.Except)
            {
                if (str.StartsWith(StringConstants.tkANY) && str != ANY.Name)
                {
                    ErrorReporter.WriteError(ErrorMessages.ANYCollision, rdec.Location);
                }
            }


            HashSet <string> Except = GetFIRST(trace, trace.Count - 1);

            foreach (string str in ANY.Except)
            {
                Except.UnionWith(symbolTable.GetFIRST(str));
            }
            ANY.Except = Except;

            RuleDeclaration rd = new RuleDeclaration();

            rd.Name  = symbolTable.GetNewName("ANY");
            ANY.Name = rd.Name;
            foreach (string str in symbolTable._tokens.Keys)
            {
                if (!ANY.Except.Contains(str))
                {
                    List <SubRulePart> branch = new List <SubRulePart>();
                    branch.Add(new SubRulePart(str, true));
                    rd.SubRules.Add(branch);
                }
            }
            symbolTable.AddRuleDeferred(rd);
        }
Exemplo n.º 2
0
        /// <summary>
        /// В ветке заменяем строковые литералы на лексемы, существующие (при наличии) или новые.
        /// </summary>
        /// <param name="branch"></param>
        private void DeclareTokensInComplexSRP(List <SubRulePart> branch)
        {
            foreach (SubRulePart srp in branch)
            {
                if (srp is SubRuleAny)
                {
                    SubRuleAny Any = srp as SubRuleAny;
                    srp.Name    = StringConstants.tkANY + AnyCounter;
                    AnyCounter += 1;

                    HashSet <string> newExcept = new HashSet <string>();
                    foreach (string str in Any.Except)
                    {
                        if (str.StartsWith(StringConstants.StringQuote))
                        {
                            newExcept.Add(symbolTable.FindOrCreateToken(str));
                        }
                        else
                        {
                            newExcept.Add(str);
                        }
                    }
                    Any.Except = newExcept;

                    continue;
                }
                SubRuleComplexPart sc = srp as SubRuleComplexPart;
                if (sc != null)
                {
                    foreach (List <SubRulePart> br2 in sc.Items)
                    {
                        DeclareTokensInComplexSRP(br2);
                    }
                }
                else
                if (srp.Name.StartsWith(StringConstants.StringQuote))
                {
                    srp.Name = symbolTable.FindOrCreateToken(srp.Name);
                }
            }
        }
Exemplo n.º 3
0
        private void CreateRuleForANY(List<Pair> trace, SubRuleAny ANY, RuleDeclaration rdec)
        {
            foreach (string str in ANY.Except)
                if (str.StartsWith(StringConstants.tkANY) && str != ANY.Name)
                    ErrorReporter.WriteError(ErrorMessages.ANYCollision, rdec.Location);


            HashSet<string> Except = GetFIRST(trace, trace.Count - 1);
            foreach (string str in ANY.Except)
                Except.UnionWith(symbolTable.GetFIRST(str));
            ANY.Except = Except;

            RuleDeclaration rd = new RuleDeclaration();
            rd.Name = symbolTable.GetNewName("ANY");
            ANY.Name = rd.Name;
            foreach (string str in symbolTable._tokens.Keys)
                if (!ANY.Except.Contains(str))
                {
                    List<SubRulePart> branch = new List<SubRulePart>();
                    branch.Add(new SubRulePart(str, true));
                    rd.SubRules.Add(branch);
                }
            symbolTable.AddRuleDeferred(rd);
        }
Exemplo n.º 4
0
        protected override void DoAction(int action)
        {
            CurrentSemanticValue = new ValueType();
#pragma warning disable 162, 1522
            switch (action)
            {
            case 2: // Program -> /* empty */
            {
                CurrentSemanticValue.sf = new SourceFile();
                root = CurrentSemanticValue.sf;
            }
            break;

            case 3: // Program -> Program, Declaration
            {
                CurrentSemanticValue.sf = ValueStack[ValueStack.Depth - 2].sf;
                if (ValueStack[ValueStack.Depth - 1].Decl != null)
                {
                    ValueStack[ValueStack.Depth - 1].Decl.Location = LocationStack[LocationStack.Depth - 1];
                    CurrentSemanticValue.sf.Declarations.Add(ValueStack[ValueStack.Depth - 1].Decl);
                }
            }
            break;

            case 4: // Declaration -> error
            {
            }
            break;

            case 5: // Declaration -> tkSkip, SkipParameters
            {
                CurrentSemanticValue.Decl = new SkipDeclaration();
                (CurrentSemanticValue.Decl as SkipDeclaration).Merge(ValueStack[ValueStack.Depth - 1].skipDs);
            }
            break;

            case 6: // Declaration -> tkRule, UseRuleName, Token, tkColon, SubRules
            {
                RuleDeclaration rd = new RuleDeclaration();
                rd.UseRuleName = ValueStack[ValueStack.Depth - 4].flag;
                rd.Name        = ValueStack[ValueStack.Depth - 3].sVal;
                rd.Merge(ValueStack[ValueStack.Depth - 1].ruleDs);
                CurrentSemanticValue.Decl = rd;
            }
            break;

            case 8: // Anon@1 -> /* empty */
            { (Scanner as Scanner).BeginRegexp(); }
            break;

            case 9: // Declaration -> tkRegExToken, Token, Anon@1, RegExp
            {
                CurrentSemanticValue.Decl = new RegExTokenDeclaration(ValueStack[ValueStack.Depth - 3].sVal, ValueStack[ValueStack.Depth - 1].sVal);
            }
            break;

            case 10: // SkipParameters -> /* empty */
            {
                CurrentSemanticValue.skipDs = new SkipDeclaration();
            }
            break;

            case 11: // SkipParameters -> SkipParameters, SkipParam
            {
                CurrentSemanticValue.skipDs = ValueStack[ValueStack.Depth - 2].skipDs;
                CurrentSemanticValue.skipDs.Merge(ValueStack[ValueStack.Depth - 1].skipDs);
            }
            break;

            case 12: // SkipParam -> tkNested
            {
                CurrentSemanticValue.skipDs        = new SkipDeclaration();
                CurrentSemanticValue.skipDs.Nested = true;
            }
            break;

            case 13: // SkipParam -> tkBegin, TokenList
            {
                CurrentSemanticValue.skipDs = new SkipDeclaration();
                CurrentSemanticValue.skipDs.Begin.AddRange(ValueStack[ValueStack.Depth - 1].sList.ToArray());
            }
            break;

            case 14: // SkipParam -> tkEnd, TokenList
            {
                CurrentSemanticValue.skipDs = new SkipDeclaration();
                CurrentSemanticValue.skipDs.End.AddRange(ValueStack[ValueStack.Depth - 1].sList.ToArray());
            }
            break;

            case 15: // SkipParam -> tkBeginEnd, TokenList
            {
                CurrentSemanticValue.skipDs = new SkipDeclaration();
                CurrentSemanticValue.skipDs.Begin.AddRange(ValueStack[ValueStack.Depth - 1].sList.ToArray());
                CurrentSemanticValue.skipDs.End.AddRange(ValueStack[ValueStack.Depth - 1].sList.ToArray());
            }
            break;

            case 16: // SkipParam -> tkEscapeSymbol, TokenList
            {
                CurrentSemanticValue.skipDs = new SkipDeclaration();
                CurrentSemanticValue.skipDs.EscapeSymbol.AddRange(ValueStack[ValueStack.Depth - 1].sList.ToArray());
            }
            break;

            case 17: // SkipParam -> tkPreprocessor
            {
                CurrentSemanticValue.skipDs = new SkipDeclaration();
                CurrentSemanticValue.skipDs.Preprocessor = true;
            }
            break;

            case 18: // TokenList -> /* empty */
            {
                CurrentSemanticValue.sList = new List <string>();
            }
            break;

            case 19: // TokenList -> TokenList, Token
            {
                CurrentSemanticValue.sList = ValueStack[ValueStack.Depth - 2].sList;
                CurrentSemanticValue.sList.Add(ValueStack[ValueStack.Depth - 1].sVal);
            }
            break;

            case 20: // TokenCommaList -> /* empty */
            {
                CurrentSemanticValue.sList = new List <string>();
            }
            break;

            case 21: // TokenCommaList -> Token
            {
                CurrentSemanticValue.sList = new List <string>();
                CurrentSemanticValue.sList.Add(ValueStack[ValueStack.Depth - 1].sVal);
            }
            break;

            case 22: // TokenCommaList -> TokenCommaList, tkComma, Token
            {
                CurrentSemanticValue.sList = ValueStack[ValueStack.Depth - 3].sList;
                CurrentSemanticValue.sList.Add(ValueStack[ValueStack.Depth - 1].sVal);
            }
            break;

            case 23: // UseNameValue -> /* empty */
            { CurrentSemanticValue.sVal = ""; }
            break;

            case 24: // UseNameValue -> tkAt
            { CurrentSemanticValue.sVal = "@"; }
            break;

            case 25: // UseNameValue -> tkSharp
            { CurrentSemanticValue.sVal = "#"; }
            break;

            case 26: // UseNameValue -> tkAt, tkSharp
            { CurrentSemanticValue.sVal = "@#"; }
            break;

            case 27: // UseNameValue -> tkSharp, tkAt
            { CurrentSemanticValue.sVal = "@#"; }
            break;

            case 28: // UseRuleName -> /* empty */
            { CurrentSemanticValue.flag = false; }
            break;

            case 29: // UseRuleName -> tkAt
            { CurrentSemanticValue.flag = true; }
            break;

            case 30: // Repetition -> /* empty */
            { CurrentSemanticValue.srRep = SubRuleRepetition.Once; }
            break;

            case 31: // Repetition -> tkQuest
            { CurrentSemanticValue.srRep = SubRuleRepetition.ZeroOrOne; }
            break;

            case 32: // Repetition -> tkPlus
            { CurrentSemanticValue.srRep = SubRuleRepetition.OneOrMore; }
            break;

            case 33: // Repetition -> tkStar
            { CurrentSemanticValue.srRep = SubRuleRepetition.ZeroOrMore; }
            break;

            case 34: // SubRuleToken -> UseNameValue, Token, Repetition
            {
                SubRulePart srp = new SubRulePart(ValueStack[ValueStack.Depth - 2].sVal);
                srp.UseName              = ValueStack[ValueStack.Depth - 3].sVal == "@" || ValueStack[ValueStack.Depth - 3].sVal == "@#";
                srp.UseValue             = ValueStack[ValueStack.Depth - 3].sVal == "#" || ValueStack[ValueStack.Depth - 3].sVal == "@#";
                srp.Repetition           = ValueStack[ValueStack.Depth - 1].srRep;
                CurrentSemanticValue.srp = srp;
            }
            break;

            case 35: // SubRuleToken -> UseNameValue, tkAny, Repetition
            {
                SubRuleAny sr = new SubRuleAny();
                sr.UseName               = ValueStack[ValueStack.Depth - 3].sVal == "@" || ValueStack[ValueStack.Depth - 3].sVal == "@#";
                sr.UseValue              = ValueStack[ValueStack.Depth - 3].sVal == "#" || ValueStack[ValueStack.Depth - 3].sVal == "@#";
                sr.Repetition            = ValueStack[ValueStack.Depth - 1].srRep;
                CurrentSemanticValue.srp = sr;
            }
            break;

            case 36: // SubRuleToken -> UseNameValue, tkAnyExcept, tkRoundOpen, ExceptList,
                     //                 tkRoundClose, Repetition
            {
                SubRuleAny sr = new SubRuleAny();
                sr.UseName               = ValueStack[ValueStack.Depth - 6].sVal == "@" || ValueStack[ValueStack.Depth - 6].sVal == "@#";
                sr.UseValue              = ValueStack[ValueStack.Depth - 6].sVal == "#" || ValueStack[ValueStack.Depth - 6].sVal == "@#";
                sr.Repetition            = ValueStack[ValueStack.Depth - 1].srRep;
                sr.Except                = ValueStack[ValueStack.Depth - 3].sHS;
                CurrentSemanticValue.srp = sr;
            }
            break;

            case 37: // SubRuleToken -> UseNameValue, tkList, tkRoundOpen, TokenCommaList, tkRoundClose,
                     //                 Repetition
            {
                if (ValueStack[ValueStack.Depth - 3].sList.Count == 0 || ValueStack[ValueStack.Depth - 3].sList.Count > 2)
                {
                    ErrorReporter.WriteError(ErrorMessages.WrongListParameters, LocationStack[LocationStack.Depth - 5]);
                }
                else
                {
                    string NT  = ValueStack[ValueStack.Depth - 3].sList[0];
                    string Sep = "";
                    if (ValueStack[ValueStack.Depth - 3].sList.Count > 1)
                    {
                        Sep = ValueStack[ValueStack.Depth - 3].sList[1];
                    }
                    SubRuleNonTermList sr = new SubRuleNonTermList(NT, Sep);
                    sr.UseName               = ValueStack[ValueStack.Depth - 6].sVal == "@" || ValueStack[ValueStack.Depth - 6].sVal == "@#";
                    sr.CanBeEmpty            = ValueStack[ValueStack.Depth - 5].sVal == StringConstants.tkList0;
                    sr.Repetition            = ValueStack[ValueStack.Depth - 1].srRep;
                    CurrentSemanticValue.srp = sr;
                }
            }
            break;

            case 38: // SubRuleToken -> tkSquareOpen, SubRules, tkSquareClose, Repetition
            {
                CurrentSemanticValue.srp            = new SubRuleComplexPart(ValueStack[ValueStack.Depth - 3].ruleDs.SubRules);
                CurrentSemanticValue.srp.Repetition = ValueStack[ValueStack.Depth - 1].srRep;
            }
            break;

            case 39: // SubRule -> /* empty */
            { CurrentSemanticValue.srpL = new List <SubRulePart>(); }
            break;

            case 40: // SubRule -> SubRule, SubRuleToken
            {
                CurrentSemanticValue.srpL = ValueStack[ValueStack.Depth - 2].srpL;
                CurrentSemanticValue.srpL.Add(ValueStack[ValueStack.Depth - 1].srp);
                ValueStack[ValueStack.Depth - 1].srp.Location = LocationStack[LocationStack.Depth - 1];
            }
            break;

            case 41: // SubRules -> SubRule
            {
                CurrentSemanticValue.ruleDs = new RuleDeclaration();
                CurrentSemanticValue.ruleDs.SubRules.Add(ValueStack[ValueStack.Depth - 1].srpL);
            }
            break;

            case 42: // SubRules -> SubRules, tkPipe, SubRule
            {
                CurrentSemanticValue.ruleDs = ValueStack[ValueStack.Depth - 3].ruleDs;
                CurrentSemanticValue.ruleDs.SubRules.Add(ValueStack[ValueStack.Depth - 1].srpL);
                CurrentSemanticValue.ruleDs.Location = LocationStack[LocationStack.Depth - 3].Merge(LocationStack[LocationStack.Depth - 1]);
            }
            break;

            case 43: // ExceptList -> Token
            { CurrentSemanticValue.sHS = new HashSet <string>(); CurrentSemanticValue.sHS.Add(ValueStack[ValueStack.Depth - 1].sVal); }
            break;

            case 44: // ExceptList -> ExceptList, tkComma, Token
            { CurrentSemanticValue.sHS = ValueStack[ValueStack.Depth - 3].sHS; CurrentSemanticValue.sHS.Add(ValueStack[ValueStack.Depth - 1].sVal); }
            break;

            case 45: // Directive -> tkCaseSensitive
            { Options.CaseInsensitive = false; }
            break;

            case 46: // Directive -> tkCaseInsensitive
            { Options.CaseInsensitive = true; }
            break;

            case 47: // Directive -> tkExtension, TokenList
            { Options.ExtensionList = ValueStack[ValueStack.Depth - 1].sList; }
            break;

            case 48: // Directive -> tkRegion, tkBegin, tkEq, Token, tkEnd, tkEq, Token
            {
                Options.RegionBegin = ValueStack[ValueStack.Depth - 4].sVal;
                Options.RegionEnd   = ValueStack[ValueStack.Depth - 1].sVal;
            }
            break;

            case 49: // Directive -> tkPreprocessor, PreProcDirective
            {}
            break;

            case 50: // Directive -> tkNamespace, Token
            { Options.Namespace = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 51: // PreProcDirective -> PreProcDirectivePart
            {}
            break;

            case 52: // PreProcDirective -> PreProcDirective, PreProcDirectivePart
            {}
            break;

            case 53: // PreProcDirectivePart -> tkDefine, tkEq, Token
            { Options.DirectiveDefine = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 54: // PreProcDirectivePart -> tkUndef, tkEq, Token
            { Options.DirectiveUndef = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 55: // PreProcDirectivePart -> tkIfdef, tkEq, Token
            { Options.DirectiveIfdef = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 56: // PreProcDirectivePart -> tkIfndef, tkEq, Token
            { Options.DirectiveIfndef = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 57: // PreProcDirectivePart -> tkElse, tkEq, Token
            { Options.DirectiveElse = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 58: // PreProcDirectivePart -> tkElif, tkEq, Token
            { Options.DirectiveElif = ValueStack[ValueStack.Depth - 1].sVal; }
            break;

            case 59: // PreProcDirectivePart -> tkEnd, tkEq, Token
            { Options.DirectiveEnd = ValueStack[ValueStack.Depth - 1].sVal; }
            break;
            }
#pragma warning restore 162, 1522
        }