예제 #1
0
파일: Parser.cs 프로젝트: Blecki/Passages
 public static Passage Parse(String file)
 {
     var state = new ParseState { start = 0, end = file.Length, source = file };
     state.activePassage = new Passage();
     ParsePassageBody(state);
     return state.activePassage;
 }
예제 #2
0
파일: Parser.cs 프로젝트: Blecki/Passages
 private static void ParseRestOfLine(out String line, ParseState state)
 {
     var t = "";
     while (!state.AtEnd() && state.Next() != '\r' && state.Next() != '\n')
     {
         t += state.Next();
         state.Advance(1);
     }
     line = t;
 }
예제 #3
0
        public static String Preprocess(String text, MISP.Environment ScriptEngine)
        {
            var state = new ParseState { start = 0, end = text.Length, source = text };
            var output = new StringBuilder();
            var preprocessContext = new PreprocessContext();
            preprocessContext.builder = output;
            var preprocessGlobals = new MISP.ScriptObject();

            while (!state.AtEnd())
            {
                while (!state.AtEnd() && !state.MatchNext("<<"))
                {
                    if (state.MatchNext("\\<<")) //Skip escaped open brackets
                    {
                        output.Append("<<");
                        state.Advance(3);
                    }
                    else
                    {
                        output.Append(state.Next());
                        state.Advance();
                    }
                }
                if (!state.AtEnd())
                {
                    state.Advance(2); //skip <<
                    var script = new StringBuilder();
                    while (!state.AtEnd() && !state.MatchNext(">>"))
                    {
                        if (state.MatchNext("\\>>"))
                        {
                            script.Append(">>");
                            state.Advance(3);
                        }
                        else
                        {
                            script.Append(state.Next());
                            state.Advance();
                        }
                    }
                    if (!state.AtEnd()) state.Advance(2); //skip >>

                    var scriptContext = ScriptEngine.CompileScript(script.ToString());
                    scriptContext.Tag = preprocessContext;
                    ScriptEngine.RunScript(scriptContext);
                    if (scriptContext.ExecutionState == MISP.ExecutionState.Error)
                    {
                        Console.WriteLine("Error in preprocessing");
                        Console.WriteLine(scriptContext.ErrorMessage);
                    }
                }
            }

            return output.ToString();
        }
예제 #4
0
파일: Parser.cs 프로젝트: Blecki/Passages
        private static bool ParsePassageBody(ParseState state)
        {
            state.activePassage.Leaf = false;

            //The summary must begin on the same line as the brace.
            ParseRestOfLine(out state.activePassage.summary, state);
            state.activePassage.summary = state.activePassage.summary.Trim();

            while (true)
            {
                DevourWhitespace(state);
                if (state.AtEnd())
                {
                    if (state.activePassage.parent != null)
                        throw new InvalidOperationException("Error 1003 at line " + state.currentLine);
                    else
                        return true;
                }
                if (state.Next() == '}')
                {
                    state.Advance(1);
                    return true;
                }
                else if (state.Next() == '{')
                    throw new InvalidOperationException("Error 1001 at line " + state.currentLine);
                else
                {
                    var line = "";
                    ParseRestOfLine(out line, state);
                    line = line.Trim();
                    if (String.IsNullOrEmpty(line)) throw new InvalidProgramException("Error 1002 at line " + state.currentLine);
                    var newPassage = new Passage();
                    newPassage.parent = state.activePassage;
                    newPassage.name = line;
                    DevourWhitespace(state);
                    if (!state.AtEnd() && state.Next() == '{')
                    {
                        state.Advance(1);
                        state.activePassage = newPassage;
                        if (!ParsePassageBody(state)) return false;
                        state.activePassage = newPassage.parent;
                    }
                    state.activePassage.children.Add(newPassage);
                }
            }
        }
예제 #5
0
파일: Parser.cs 프로젝트: Blecki/Passages
 private static void ParseToken(out String token, ParseState state)
 {
     var t = "";
     while (!IsWhitespace(state.Next()))
     {
         t += state.Next();
         state.Advance(1);
     }
     token = t;
 }
예제 #6
0
파일: Parser.cs 프로젝트: Blecki/Passages
 private static void DevourWhitespace(ParseState state)
 {
     while (!state.AtEnd() && " \t\r\n".Contains(state.Next())) state.Advance();
 }
예제 #7
0
파일: Parser.cs 프로젝트: Blecki/Passages
 private static void DevourSpaces(ParseState state)
 {
     while (!state.AtEnd() && " \t".Contains(state.Next())) state.Advance();
 }