protected override bool SetCore(IErrorReporter reporter, IToken valueToken)
        {
            var text = valueToken.Text;

            if (valueToken.Type == SettingTokenType.Label)
            {
                switch (text)
                {
                case "true":
                    Value = true;
                    return(true);

                case "false":
                    Value = false;
                    return(false);

                default:
                    ReporterHelper.AddError(reporter, valueToken, "'{0}' is not a valid value.", text);
                    return(false);
                }
            }
            else
            {
                ReporterHelper.AddError(reporter, valueToken, "\"{0}\" is not a valid value.", text);
                return(false);
            }
        }
        void IConfigCommandVisitor.Visit(ConfigCommandArg argValue)
        {
            var token = argValue.Value;

            if (!int.TryParse(token.Text, out var argIndex) || argIndex >= _production.Segments.Length)
            {
                ReporterHelper.AddError(_reporter, token, "Invalid argument reference");
            }
        }
Exemple #3
0
        protected override bool SetCore(IErrorReporter reporter, IToken valueToken)
        {
            if (valueToken.Type == SettingTokenType.Label)
            {
                Value = valueToken.Text;
                return(true);
            }

            ReporterHelper.AddError(reporter, valueToken, "'{0}' is not a valid value.", valueToken.Text);
            return(false);
        }
        bool VerifyProductionReferences()
        {
            var success = true;

            var ntReferences  = new Dictionary <Segment, List <ConfigToken> >();
            var ntDefinitions = new Dictionary <Segment, List <ConfigToken> >();

            foreach (var entryPoint in _config.EntryPoints)
            {
                AddToList(ntReferences, entryPoint.Segment, entryPoint.NonTerminal);
            }

            foreach (var nonTerminal in _config.Productions)
            {
                AddToList(ntDefinitions, nonTerminal.Segment, nonTerminal.Target);

                foreach (var rule in nonTerminal.Rules)
                {
                    foreach (var segment in rule.Segments)
                    {
                        if (segment.Token.Type == ConfigTokenType.NonTerminal)
                        {
                            AddToList(ntReferences, segment.Segment, segment.Token);
                        }
                    }
                }
            }

            foreach (var pair in ntDefinitions)
            {
                ntReferences.Remove(pair.Key);

                var list = pair.Value;

                for (var i = 1; i < list.Count; i++)
                {
                    var token = list[i];
                    ReporterHelper.AddWarning(_reporter, token, "The non-terminal <{0}> has already been defined.", token.Text);
                }
            }

            foreach (var pair in ntReferences)
            {
                foreach (var token in pair.Value)
                {
                    ReporterHelper.AddError(_reporter, token, "The non-terminal <{0}> is not defined.", token.Text);
                    success = false;
                }
            }

            return(success);
        }
Exemple #5
0
        Graph BuildDFA(Dictionary <string, int> typeLookup)
        {
            var nfa = new Graph <NodeData, CharSet> .Builder();

            for (var i = 0; i < _config.States.Count; i++)
            {
                var state = _config.States[i];

                if (state.Rules.Count == 0)
                {
                    ReporterHelper.AddError(_reporter, state.Label, "The state '{0}' does not define any rules.", state.Label.Text);
                    continue;
                }

                var startState = nfa.NewState(true, new NodeData(i, null));

                for (var j = 0; j < state.Rules.Count; j++)
                {
                    var       rule = state.Rules[j];
                    ReElement element;

                    if (!typeLookup.TryGetValue(rule.Token.Text, out var endStateID))
                    {
                        endStateID = typeLookup.Count;
                        typeLookup.Add(rule.Token.Text, endStateID);
                    }

                    try
                    {
                        element = ReParser.Parse(rule.Regex.Text);
                    }
                    catch (ReParseException ex)
                    {
                        ReporterHelper.AddError(_reporter, rule.Regex, ex.Message);
                        continue;
                    }

                    if (element.MatchesEmptyString)
                    {
                        ReporterHelper.AddWarning(
                            _reporter,
                            rule.Regex,
                            "This regular expression claims to match the empty string, " +
                            "this is not supported and usually indicates a typeo in the regular expression");
                    }

                    element.GenerateNFA(nfa, startState, nfa.NewState(false, new NodeData(null, endStateID, j)));
                }
            }

            return(FATools.CreateDfa(nfa.Graph));
        }
Exemple #6
0
 void ReportUnexpectedToken(ConfigToken token)
 {
     if (token.Type == ConfigTokenType.Error)
     {
         ReporterHelper.AddError(_reporter, token, token.Text);
     }
     else if (token.Type == ConfigTokenType.EOF)
     {
         ReporterHelper.AddError(_reporter, token, "Unexpected end of file.");
     }
     else
     {
         ReporterHelper.AddError(_reporter, token, "Unexpected Token.");
     }
 }
        static void ReportReduceReduceConflict(Config config, List <Production> productions, IErrorReporter reporter)
        {
            var rules  = new ConfigRule[productions.Count];
            var starts = new int[productions.Count];

            for (var i = 0; i < rules.Length; i++)
            {
                var production = productions[i];

                if (production.Target.IsInitial)
                {
                    starts[i] = -1;
                }
                else
                {
                    var rule = config.RuleLookup[production];
                    rules[i]  = rule;
                    starts[i] = rule.FromPos.Index;
                }
            }

            Array.Sort(starts, rules);

            string format;
            string val;

            if (rules[0] == null)
            {
                format = "{0} causes a reduce-accept conflict.";
                val    = null;
            }
            else
            {
                format = "{0} causes a reduce-reduce conflict with {1}.";
                val    = rules[0].Production.ToString();
            }

            for (var i = 1; i < rules.Length; i++)
            {
                ReporterHelper.AddError(reporter, rules[i], format, rules[i].Production, val);
            }
        }
        void PopulateUsings()
        {
            var usingLookup = new Dictionary <string, ConfigUsing>();

            foreach (var cUsing in _config.Usings)
            {
                var label = cUsing.Label.Text;

                if (!usingLookup.ContainsKey(label))
                {
                    usingLookup.Add(label, cUsing);
                }
                else
                {
                    ReporterHelper.AddError(_reporter, cUsing.Label, "Type reference already defined.");
                }
            }

            {
                var label = _config.Manager.TokenType;

                if (!string.IsNullOrEmpty(label) && usingLookup.TryGetValue(label, out var terminalType))
                {
                    _config.TerminalType = terminalType;
                }
            }

            var pending = new List <Segment>();

            foreach (var cProduction in _config.Productions)
            {
                var name    = cProduction.Target.Text;
                var segment = GetNonTerminal(name);
                cProduction.Segment = segment;

                if (cProduction.TypeRef == null)
                {
                    pending.Add(segment);
                }
                else
                {
                    if (!usingLookup.TryGetValue(cProduction.TypeRef.Text, out var cUsing))
                    {
                        ReporterHelper.AddError(_reporter, cProduction.TypeRef, "Unknown type reference.");
                    }
                    else if (!_ntTypes.TryGetValue(segment, out var cExistingUsing))
                    {
                        _ntTypes.Add(segment, cUsing);
                    }
                    else if (cUsing != cExistingUsing)
                    {
                        ReporterHelper.AddError(_reporter, cProduction.TypeRef, "This type conflicts with a previous definition of <{0}>.", name);
                    }
                }
            }

            ConfigUsing defaultUsing = null;

            if (_config.TerminalType == null)
            {
                defaultUsing = CreateDefaultUsing(usingLookup);
                _config.Usings.Add(defaultUsing);
                _config.TerminalType = defaultUsing;
            }

            foreach (var segment in pending)
            {
                if (_ntTypes.ContainsKey(segment))
                {
                    continue;
                }

                if (defaultUsing == null)
                {
                    defaultUsing = CreateDefaultUsing(usingLookup);
                    _config.Usings.Add(defaultUsing);
                }

                _ntTypes.Add(segment, defaultUsing);
            }
        }