Example #1
0
 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;
 }
Example #2
0
 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;
 }
Example #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();
        }
Example #4
0
        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);
                }
            }
        }
Example #5
0
 private static void ParseToken(out String token, ParseState state)
 {
     var t = "";
     while (!IsWhitespace(state.Next()))
     {
         t += state.Next();
         state.Advance(1);
     }
     token = t;
 }
Example #6
0
 private static void DevourWhitespace(ParseState state)
 {
     while (!state.AtEnd() && " \t\r\n".Contains(state.Next())) state.Advance();
 }
Example #7
0
 private static void DevourSpaces(ParseState state)
 {
     while (!state.AtEnd() && " \t".Contains(state.Next())) state.Advance();
 }