Example #1
0
        public override Verb CreateVerb(string[] tokens)
        {
            Color(position, length, KeyWords);

            var endOfLineParser = new EndOfLineParser();

            endOfLineParser.Scan(source, NextPosition);
            var index = endOfLineParser.Position;

            if (index == -1)
            {
                return(null);
            }

            if (GetBlock(source, index, true) is Some <(Block, int)> some)
            {
                (var b, var i)     = some.Value;
                overridePosition   = i;
                block              = b;
                block.AutoRegister = false;
                return(new NullOp());
            }

            return(null);
        }
Example #2
0
        public override Verb CreateVerb(string[] tokens)
        {
            var sourceRecord = tokens[5];
            var oneLine      = tokens[2] == "(";

            Color(position, tokens[1].Length, Whitespaces);
            Color(tokens[2].Length, Structures);
            Color(tokens[3].Length, KeyWords);
            Color(tokens[4].Length, KeyWords);
            Color(sourceRecord.Length, Variables);

            var index   = NextPosition;
            var members = new Hash <string, Thunk>();

            if (oneLine)
            {
                var parser = new OneLineMemberParser();
                while (index < source.Length && parser.Scan(source, index))
                {
                    members[parser.MemberName] = parser.Thunk;
                    if (freeParser.Scan(source, parser.Position, "^ |sp| ','"))
                    {
                        index = freeParser.Position;
                        continue;
                    }

                    if (freeParser.Scan(source, parser.Position, "^ |sp| ')'"))
                    {
                        overridePosition = freeParser.Position;
                        return(new CreateRecord(members, sourceRecord));
                    }
                }
            }
            else
            {
                var endOfLineParser = new EndOfLineParser();
                if (endOfLineParser.Scan(source, index))
                {
                    index = endOfLineParser.Position;
                }

                AdvanceTabs();

                var parser = new MultiLineMemberParser();
                while (index < source.Length && parser.Scan(source, index))
                {
                    index = parser.Position;
                    members[parser.MemberName] = parser.Thunk;
                }

                RegressTabs();

                overridePosition = index;
                return(new CreateRecord(members, sourceRecord));
            }

            return(null);
        }
Example #3
0
        public override Verb CreateVerb(string[] tokens)
        {
            var fieldName = tokens[2];

            Color(position, tokens[1].Length, KeyWords);
            Color(fieldName.Length, Variables);
            Color(tokens[3].Length, Structures);

            if (GetExpression(source, NextPosition, PassAlong(REGEX_GUARD_OR_END, false)).If(out var expression, out var index))
            {
                var currentIndex = index;
                var guardBlock   = none <Block>();
                if (freeParser.Scan(source, currentIndex, "^ |sp| 'guard' /b"))
                {
                    freeParser.ColorAll(KeyWords);
                    currentIndex = freeParser.Position;
                    if (GetExpression(source, currentIndex, EndOfLine()).If(out var guard, out var i))
                    {
                        currentIndex = i;
                        guardBlock   = guard.Some();
                    }
                }

                if (endOfLineParser.Scan(source, currentIndex))
                {
                    currentIndex = endOfLineParser.Position;
                }

                if (GetBlock(source, currentIndex, true).If(out var ifTrue, out var j))
                {
                    currentIndex = j;
                    var ifFalse = none <Block>();
                    if (guardBlock.IsNone && freeParser.Scan(source, currentIndex, "^ /(|tabs| 'else') (/r /n | /r | /n) "))
                    {
                        freeParser.ColorAll(KeyWords);
                        currentIndex = freeParser.Position;
                        if (GetBlock(source, currentIndex, true).If(out var elseBlock, out var elseIndex))
                        {
                            currentIndex = elseIndex;
                            guardBlock   = elseBlock.Some();
                        }
                    }

                    if (guardBlock.IsSome)
                    {
                        ifTrue.LastIsReturn.Must().BeTrue().OrThrow("Maybe", () => "return required");
                    }

                    overridePosition = currentIndex;
                    return(new Maybe(fieldName, expression, ifTrue, ifFalse, guardBlock)
                    {
                        Index = position
                    });
                }
            }

            return(null);
        }
Example #4
0
        public override bool Continue(Parser parser, string source)
        {
            if (result.Position >= source.Length)
            {
                return(true);
            }

            if (endOfLineParser.Scan(source, result.Position))
            {
                overridePosition = endOfLineParser.Result.Position;
            }
            return(true);
        }
Example #5
0
        protected EndOfLineType matchEndOfLine()
        {
            if (NextPosition < source.Length)
            {
                var endOfLineParser = new EndOfLineParser();
                if (endOfLineParser.Scan(source, NextPosition))
                {
                    overridePosition = endOfLineParser.Position;
                    return(EndOfLineType.EndOfLine);
                }

                return(EndOfLineType.More);
            }

            return(EndOfLineType.EndOfSource);
        }
Example #6
0
        public override Verb CreateVerb(string[] tokens)
        {
            var moduleName = tokens[2];

            Color(position, tokens[1].Length, KeyWords);
            Color(moduleName.Length, Variables);

            var endLineParser = new EndOfLineParser();

            endLineParser.Scan(source, NextPosition);
            var index = endLineParser.Position;

            if (GetBlock(source, index, true).If(out var block, out var i))
            {
                overridePosition = i;
                return(new CreateNewModule(moduleName, new NewModule(block)));
            }

            return(null);
        }
Example #7
0
        public override Verb CreateVerb(string[] tokens)
        {
            Color(position, tokens[1].Length, KeyWords);
            var name = tokens[2];

            Color(name.Length, Types);
            var index = NextPosition;

            if (doesParser.Scan(source, index))
            {
                traits = doesParser.Traits;
                index  = doesParser.Position;
            }

            if (endOfLineParser.Scan(source, index))
            {
                index = endOfLineParser.Position;
            }

            var members = new Hash <string, Value>();

            InClassDefinition = true;
            foreach (var traitName in traits)
            {
                var trait = CompilerState.Trait(traitName).Must().HaveValue().Force(LOCATION, () => $"Trait {traitName} isn't defined");
                foreach (var(key, value) in trait.Members)
                {
                    members[key] = value;
                }
            }

            try
            {
                AdvanceTabs();
                while (index < source.Length)
                {
                    if (bodyParser.Scan(source, index) && bodyParser.Parser.If(out var parser))
                    {
                        index = parser.Position;
                        if (parser is ITraitName aTraitName)
                        {
                            var traitName  = aTraitName;
                            var memberName = traitName.MemberName;
                            var value      = parser.Result.Value;
                            if (value != null)
                            {
                                members[memberName] = value;
                            }
                            else
                            {
                                value = traitName.Getter;
                                if (value != null)
                                {
                                    members[GetterName(memberName)] = value;
                                }

                                value = traitName.Setter;
                                if (value != null)
                                {
                                    members[SetterName(memberName)] = value;
                                }

                                members[MangledName(memberName)] = new Null();
                            }
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            finally
            {
                InClassDefinition = false;
                RegressTabs();
            }

            var newTrait = new Trait(name, members);

            overridePosition = index;

            CompilerState.RegisterTrait(newTrait);

            return(new CreateTrait(newTrait, traits)
            {
                Index = position
            });
        }