internal override int ParseMethod(ParsingSession session, out object result)
        {
            object identifier, value;
            int returnValue = -1;

            result = null;

            // dick
            if (identifierParser.Run (session, out identifier) > -1) {
                // =
                if (Couple(session, ref identifier)) {
                    // "butt";
                    session.DeepenContext(identifier as String);
                    int iresult = InnerParser.Run (session, out value);
                    if (iresult > 0) {
                        session.ContextRegister(value);
                        session.SurfaceContext(identifier as String);
                        result = new Tuple<string, object> ((string)identifier, value);
                        returnValue = 1;
                    } else {
                        session.SurfaceContext(identifier as String);
                    }
                }
            }

            return returnValue;
        }
        internal override int ParseMethod(ParsingSession session, out object result)
        {
            int resultCount;
            object identObj;
            string identifier;
            object valueObj;
            string value;

            result = null;

            resultCount = base.ParseMethod (session, out dummy);

            if (hashtagEater.ParseMethod (session, out dummy) > 0) {
                if (identifierEater.ParseMethod (session, out identObj) < 0)
                    throw new ParsingException (session, identifierEater, session.Trail);

                if (base.ParseMethod (session, out dummy) < 1)
                    throw new ParsingException (session, this, session.Trail);

                if (valueEater.ParseMethod (session, out valueObj) < 0)
                    throw new ParsingException (session, valueEater, session.Trail);

                identifier = (string)identObj;
                value = (string)valueObj;

                if (identifier.ToLower () == "include")
                    resultCount = IncludeFileIntoSession (value, session);

                if (identifier.ToLower() == "pwd") {
                    Directory.SetCurrentDirectory(value);
                }
            }

            return resultCount;
        }
        internal override int ParseMethod(ParsingSession session, out object result)
        {
            var headDefinition = Parse<Settings>.Using (HeadParser, session);
            var baseDefinitions = Parse<List<Settings>>.Using (PredefParser, session);
            var chainedDefinition = Parse<Settings>.Using (ChainParser, session);
            var bodyDefinition = Parse<Settings>.Using (BodyParser, session);
            var successorDefinition = Parse<Settings>.Using (SuccessorParser, session);

            if (chainedDefinition != null) {
                headDefinition [Syntax.ChainSubstitution] = chainedDefinition;
            }
            if (successorDefinition != null) {
                headDefinition [Syntax.SuccessorSubstitution] = successorDefinition;
            }

            baseDefinitions.Add (headDefinition);
            baseDefinitions.Add (bodyDefinition);

            var cleanDefinitions = baseDefinitions.FindAll (delegate(Settings obj) {
                return obj != null;
            });
            cleanDefinitions.Reverse ();

            result = Settings.FromMerge (cleanDefinitions.ToArray());

            return 1;
        }
        public ParsingException(ParsingSession session, Parser parser, string after = "(not given)", string before = "(not given)")
            : base(string.Format(
				"Expected {0} at line {1}, offset {2}, col {3}, after {4}, before {5}", 
					parser.ToString(), 
					session.CurrentLine.ToString(), 
					session.Offset.ToString(),
					session.CurrentColumn.ToString(),
					after, before))
        {
        }
        protected override bool Couple(ParsingSession session, ref object identifier)
        {
            if (!base.Couple (session, ref identifier)) {
                foreach (CharacterParser parser in couplerParsers) {
                    if (parser.Run (session) < 0) {
                        return false;
                    }
                }

                identifier = string.Concat (identifier, this.CouplerSuffix);
            }

            return true;
        }
        internal override int ParseMethod(ParsingSession session, out object result)
        {
            int filenameLength = -1;

            result = null;

            if (opener.ParseMethod (session, out dummy) > 0) {
                if (this.innerParser.ParseMethod (session, out result) > 0) {
                    if (closer.ParseMethod (session, out dummy) > 0) {
                        filenameLength = ((string)result).Length;
                    }
                }
            }

            return filenameLength;
        }
        protected override int ParseListBody(ParsingSession session, ref List<object> target)
        {
            object defaultValue;
            bool thereIsMore = true;

            if (valueParser.ParseMethod (session, out defaultValue) > -1) {
                target.Add (new Tuple<string, object> ("default", defaultValue));
                thereIsMore = (coupler.Run(session) > 0);
            }

            if (thereIsMore) {
                return base.ParseListBody (session, ref target);
            } else {
                return closer.Run(session);
            }
        }
        /// <summary>
        /// Includes a file into a session.
        /// </summary>
        /// <returns>
        /// The included file length
        /// </returns>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='session'>
        /// Session.
        /// </param>
        int IncludeFileIntoSession(string fileName, ParsingSession session)
        {
            if (File.Exists (fileName)) {
                FileInfo info = new FileInfo(fileName);

                string fileData;

                using(StreamReader reader = new StreamReader(info.OpenRead()))
                {
                    fileData = reader.ReadToEnd();
                }

                session.Data = session.Data.Insert (
                    session.Offset,
                    fileData);

                return fileData.Length;
            } else {
                throw new Exception ("During parsing, file not found: " + fileName);
            }
        }
 protected virtual bool Couple(ParsingSession session, ref object identifier)
 {
     return coupler.Run (session) > -1;
 }
 /// <summary>
 /// Reads all contents from a file for usage in a ParsingSession, and uses
 /// and alternative parser for whitespace regions (i.e. #operations)
 /// </summary>
 /// <returns>The file.</returns>
 /// <param name="file">File.</param>
 /// <param name="whitespaceParser">Whitespace parser.</param>
 public static ParsingSession FromFile(string file, Parser whitespaceParser, bool profilingEnabled = false)
 {
     ParsingSession result = new ParsingSession (File.ReadAllText (file), whitespaceParser, profilingEnabled);
     result.SourceFile = new FileInfo (file);
     result.WorkingDirectory = result.SourceFile.Directory.FullName;
     return result;
 }
        /// <summary>
        /// Run the specified session and result.
        /// </summary>
        /// <param name='session'>
        /// Session.
        /// </param>
        /// <param name='result'>
        /// Result.
        /// </param>
        public int Run(ParsingSession session, out object result)
        {
            result = null;
            // this is never good
            if (session.Offset >= session.Data.Length)
                return -1;

            if ((session.whitespaceParser != null) && (this != session.whitespaceParser))
                session.whitespaceParser.Run (session, out dummy);

            if (session.ProfilingEnabled)
                return ProfiledParseMethod (session, out result);
            else
                return ParseMethod (session, out result);
        }
 /// <summary>
 /// Run the Parser using the specified Session.
 /// </summary>
 /// <param name='session'>
 /// Session to use.
 /// </param>
 public int Run(ParsingSession session)
 {
     return Run(session, out dummy);
 }
        internal int ProfiledParseMethod(ParsingSession session, out object result)
        {
            int resultCode = 0;
            object aresult = null;

            session.ParsingProfiler.CheckIn (GetProfileName(), delegate() {
                resultCode = ParseMethod(session, out aresult);
            });

            result = aresult;

            return resultCode;
        }
 /// <summary>
 /// Method which parses data from session into resulting object
 /// </summary>
 /// <returns>
 /// Success value, greater than -1 when succesful.
 /// </returns>
 /// <param name='session'>
 /// ParsingSession to get data from.
 /// </param>
 /// <param name='result'>
 /// Result of Parse Action, if any.
 /// </param>
 internal abstract int ParseMethod(ParsingSession session, out object result);