Beispiel #1
0
        public static bool TryScan(CodeReader reader, out ForCommand command)
        {
            command = null;
            string line = reader.NextLine();

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string         variable  = line.Split(':')[0];
            int            min       = int.Parse(line.Split('(', ' ')[2]);
            int            max       = int.Parse(Regex.Match(line, " to " + RegExHelper.Integer + " in ").Value.Split(new string[] { " to ", " in " }, StringSplitOptions.RemoveEmptyEntries)[0]);
            int            increment = 1;  //TODO
            List <Command> commands  = new List <Command>();

            line = reader.NextLine();
            while (!reader.Done && line != "endfor")
            {
                if (TryScan(line, out Command cmd))
                {
                    commands.Add(cmd);
                }
                else
                {
                    throw new ArgumentException();
                }
                line = reader.NextLine();
            }
            command = new ForCommand(variable, new CommandCollection(commands.ToArray()), min, max, increment);
            return(true);
        }
Beispiel #2
0
        public static bool TryScan(CodeReader reader, out Pattern scanned)
        {
            scanned = null;
            string line = reader.NextLine();

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string name = line.Substring("pattern ".Length).Trim(':');

            scanned = new Pattern(name);
            line    = reader.NextLine();
            while (!reader.Done && !reader.EndingKeyword)
            {
                if (Command.TryScan(reader.Copy(), out Command command))
                {
                    scanned.AddCommand(command);
                    reader.Skip(command.Lines - 1);
                    line = reader.NextLine();
                }
                else
                {
                    throw new Exception("Unknown Command in Line " + reader.TextLine + ".");
                }
            }
            return(true);
        }
Beispiel #3
0
        public static bool TryScan(CodeReader reader, out Style scanned)
        {
            scanned = null;
            string line = reader.NextLine();

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string name = Regex.Match(line, @"(std:|" + RegExHelper.Variable + @"\()").Value;

            name    = name.Remove(name.Length - 1);
            scanned = new Style(name);
            line    = reader.NextLine();
            while (!reader.Done && !reader.EndingKeyword)
            {
                if (Instruction.TryScan(reader.Copy(), out Instruction instruction))
                {
                    scanned.AddInstruction(instruction);
                    line = reader.NextLine();
                }
                else if (StaticFunctionCallCommand.TryScan(line, out StaticFunctionCallCommand command))
                {
                    scanned.AddCommand(command);
                    line = reader.NextLine();
                }
                else
                {
                    throw new Exception("Unkwon Command in Line " + reader.CurrentLine + ".");
                }
            }
            return(true);
        }
Beispiel #4
0
        public static bool TryScan(CodeReader reader, out CaseCommand command)
        {
            command = null;
            string line = reader.NextLine().Trim('\r');

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            if (TryScan(line.Remove(line.Length - 1).Substring("case ".Length), out Command condition))
            {
                line = reader.NextLine().Trim('\r');
                List <Command> body = new List <Command>();
                while (line != "endswitch" && !Regex.IsMatch(line, RegEx) && !reader.Done)
                {
                    if (TryScan(line, out Command cmd))
                    {
                        body.Add(cmd);
                    }
                    else
                    {
                        throw new Exception("Unexpected Command in switch-case!");
                    }
                    line = reader.NextLine().Trim('\r');
                }
                command = new CaseCommand(condition, new CommandCollection(body.ToArray()));
                return(true);
            }
            return(false);
        }
Beispiel #5
0
        public static bool TryScan(CodeReader reader, Presentation parent, out Slide slide)
        {
            slide = null;
            string line = reader.NextLine();

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string name = Regex.Match(line, RegExHelper.String).Value.Trim('@', '\'');

            slide = new Slide(name, parent);
            line  = reader.NextLine();
            Step normalStep = null;

            while (!reader.Done && !reader.EndingKeyword)
            {
                if (Step.TryScan(reader.Copy(), slide, out Step step))
                {
                    if (normalStep != null)
                    {
                        slide.AddStep(normalStep);
                    }
                    normalStep = null;
                    reader.Skip(step.LineLength - 1);
                    slide.AddStep(step);
                }
                else
                {
                    if (normalStep == null)
                    {
                        normalStep = new Step("normal", slide);
                    }
                    if (Command.TryScan(reader.Copy(), out Command command))
                    {
                        reader.Skip(command.Lines - 1);
                        normalStep.Add(command);
                    }
                    else if (StaticFunctionCallCommand.TryScan(line, out StaticFunctionCallCommand functionCallCommand))
                    {
                        reader.Skip(functionCallCommand.Lines - 1);
                        normalStep.Add(functionCallCommand);
                    }

                    else
                    {
                        throw new ArgumentException("Unknown Command in Line " + reader.CurrentLine + ".");
                    }
                }
                line = reader.NextLine();
            }
            if (normalStep != null)
            {
                slide.AddStep(normalStep);
            }
            return(true);
        }
Beispiel #6
0
        public static bool TryScan(CodeReader reader, Slide parent, out Step scanned)
        {
            scanned = null;
            string line = reader.NextLine();

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string name = Regex.Match(line, RegExHelper.String).Value.Trim('\'');

            scanned = new Step(name, parent);
            line    = reader.NextLine();

            while (!reader.Done && !reader.EndingKeyword && !Regex.IsMatch(line, RegEx))
            {
                if (String.IsNullOrWhiteSpace(line))
                {
                    line = reader.NextLine();
                    continue;
                }
                if (Command.TryScan(reader.Copy(), out Command command))
                {
                    reader.Skip(command.Lines - 1);
                    scanned.Add(command);
                }
                else if (StaticFunctionCallCommand.TryScan(line, out StaticFunctionCallCommand functionCallCommand))
                {
                    reader.Skip(functionCallCommand.Lines - 1);
                    scanned.Add(functionCallCommand);
                }
                else
                {
                    throw new ArgumentException("Unknwon Command in Line " + reader.TextLine + ".");
                }
                line = reader.NextLine();
            }
            return(true);
        }
Beispiel #7
0
        public static bool TryScan(CodeReader reader, out AssignmentCommand command)
        {
            command = null;
            string line = reader.NextLine().Trim(';');

            if (!Regex.IsMatch(line, RegEx))
            {
                return(false);
            }
            string name  = line.Split(':')[0];
            string value = String.Join(":", line.Split(':').Skip(1));

            if (TryScan(value, out Command constructor))
            {
                command = new AssignmentCommand(name, constructor, constructor.ReturnType);
                return(true);
            }
            return(false);
        }
Beispiel #8
0
        public static bool TryScan(CodeReader reader, out Command scanned)
        {
            scanned = null;
            string line = reader.NextLine().Trim(';');

            if (Regex.IsMatch(line, LoopCommand.RegEx))
            {
                string loop   = line.Split(':')[0];
                string fpsStr = Regex.Match(loop, RegExHelper.Integer + "fps").Value;
                if (!int.TryParse(fpsStr.Trim('f', 'p', 's'), out int fps))
                {
                    fps = 30;
                }
                int    times    = -1;
                string timesStr = Regex.Match(loop, RegExHelper.Integer + "(" + RegExHelper.Time + ")?").Value;
                if (!int.TryParse(Regex.Match(timesStr, RegExHelper.Integer).Value, out times))
                {
                    times = -1;
                }
                if (TryScan(line.Substring(line.IndexOf(':') + 1), out Command cmd))
                {
                    scanned = new LoopCommand(cmd, fps, times);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, FunctionCallCommand.RegEx))
            {
                string obj        = line.Split('.')[0];
                string method     = line.Split('.', '(')[1];
                string parameters = line.Substring(line.IndexOf(method) + method.Length);
                if (TryScan(parameters, out Command cmd))
                {
                    scanned = new FunctionCallCommand(obj, method, (ParameterCommand)cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(SetPropertyCommand.RegEx)))
            {
                string obj      = line.Split('.')[0];
                string property = line.Split('.', ':')[1];
                string value    = line.Substring(line.IndexOf(property) + property.Length + 1);
                if (TryScan(value, out Command cmd))
                {
                    scanned = new SetPropertyCommand(obj, property, cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, Command.RegExWholeLine(AssignmentCommand.RegEx)))
            {
                if (AssignmentCommand.TryScan(reader.Copy(), out AssignmentCommand cmd))
                {
                    scanned = cmd;
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(SetArrayCommand.RegEx)))
            {
                string variable = line.Split('[')[0];
                string number   = line.Split('[', ']')[1];
                string command  = string.Join(":", line.Split(':').Skip(1)).Trim();
                if (Command.TryScan(command, out Command val))
                {
                    if (int.TryParse(number, out int index))
                    {
                        scanned = new SetArrayCommand(variable, index, val);
                        return(true);
                    }
                }
            }
            if (Regex.IsMatch(line, ForCommand.RegEx))
            {
                if (ForCommand.TryScan(reader.Copy(), out ForCommand cmd))
                {
                    scanned = cmd;
                    return(true);
                }
            }
            if (Regex.IsMatch(line, GotoCommand.RegEx))
            {
                throw new NotImplementedException();
            }
            if (Regex.IsMatch(line, IfCommand.Regex))
            {
                if (TryScan(line.Split('(', ')')[1], out Command condition))
                {
                    List <Command> body = new List <Command>();
                    while (line != "endif" && !reader.Done)
                    {
                        line = reader.NextLine();
                        if (TryScan(reader.Copy(), out Command cmd))
                        {
                            body.Add(cmd);
                        }
                        else
                        {
                            throw new Exception("Unknown Command at Line " + reader.TextLine + ".");
                        }
                        line = reader.NextLine().Trim();
                    }
                    scanned = new IfCommand(condition, new CommandCollection(body.ToArray()));
                    return(true);
                }
            }
            if (Regex.IsMatch(line, SwitchCommand.Regex))
            {
                if (TryScan(line.Split('(', ')')[1], out Command condition))
                {
                    List <CaseCommand> body = new List <CaseCommand>();
                    while (line != "endswitch" && !reader.Done)
                    {
                        if (Regex.IsMatch(line, CaseCommand.RegEx))
                        {
                            if (CaseCommand.TryScan(reader.Copy(), out CaseCommand cmd))
                            {
                                body.Add(cmd);
                            }
                            else
                            {
                                throw new Exception("Unknown Command at Line " + reader.TextLine + ".");
                            }
                        }
                        line = reader.NextLine().Trim();
                    }
                    scanned = new SwitchCommand(condition, body.ToArray());
                    return(true);
                }
            }
            if (Regex.IsMatch(line, WhileCommand.Regex))
            {
                throw new NotImplementedException();
            }
            if (Regex.IsMatch(line, RegExWholeLine(StyleCommand.RegEx)))
            {
                if (StyleValue.TryScan(line, out StyleValue styleValue))
                {
                    scanned = new StyleCommand(styleValue);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, ApplyStyleCommand.RegEx))
            {
                if (line.Count(c => c == '.') > 0)
                {
                    Console.WriteLine("ApplyStyleCommand: " + line);
                    string lastHalf  = line.Split('.').Last();
                    string firstHalf = line.Remove(line.Length - lastHalf.Length - 1);
                    string name      = lastHalf.Trim('(', ')');
                    if (TryScan(firstHalf, out Command cmd))
                    {
                        scanned = new ApplyStyleCommand(Style.GetByName(name), cmd);
                        return(true);
                    }
                }
                else
                {
                    Console.WriteLine("ApplyStyleCommand: " + line);
                }
            }
            //if (Regex.IsMatch(line, ValueCommand.RegEx))
            //	throw new NotImplementedException();
            //if (Regex.IsMatch(line, CommandCollection.Regex))
            //	throw new NotImplementedException();
            return(false);
        }
Beispiel #9
0
        public static bool TryScan(CodeReader reader, out Instruction scanned)
        {
            string line = reader.NextLine().Trim(';');

            return(TryScan(line, out scanned));
        }