Exemplo n.º 1
0
        private void ParseParam(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (line.Length <= ParamDirective.Length + 1 ||
                line[ParamDirective.Length] != ' ')
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.ParamDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            Match m = AtParser.paramExpr.Match(line, ParamDirective.Length);

            if (!m.Success)
            {
                TemplateParsingException ex = new TemplateParsingException(
                    Resources.ParamDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            Parameter p = new Parameter(
                m.Groups["paramName"].Value,
                m.Groups["paramType"].Value.TrimEnd());

            this.ast.Head.Parameters.Add(p);

            if (this.debug)
            {
                p.Line = this.lineCount;
            }
        }
Exemplo n.º 2
0
        private void ParseOutputStart(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            string trimedLine = line.TrimStart();
            string key        = trimedLine.Substring(OutputStartDirective.Length).Trim();

            if (trimedLine.Length == OutputStartDirective.Length ||
                trimedLine[OutputStartDirective.Length] != ' ' ||
                key.Length == 0)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.NoOutputKeyProvided);
                FillParseError(ex);
                throw ex;
            }

            Output output = new Output(key);

            this.currentDirective.Directives.Add(output);
            this.currentDirective = output;

            this.outputStack.Push(
                new OutputContext(
                    key,
                    line.Substring(0, CountLeadingWhiteSpace(line))));

            this.contextStack.Push(new Context(TemplateMode.Static, ""));
        }
Exemplo n.º 3
0
        private void ParseSectionRef(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            int    index  = line.IndexOf(SectionRefDirective);
            string spaces = line.Substring(0, index);
            string text   = line.Substring(
                index + SectionRefDirective.Length);

            Match match = nameExp.Match(text);

            if (!match.Success)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.BadSectionRef);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective.Directives.Add(
                new SectionReference(
                    match.Groups["name"].Value,
                    match.Groups["rest"].Value.Trim(),
                    spaces,
                    this.lineCount));
        }
Exemplo n.º 4
0
        private void FillParseError(TemplateParsingException ex)
        {
            Assertion.Assert(ex != null, "ex cannot be null.");

            ex.SourceLine = this.lineCount;

            if (this.templateFile != null)
            {
                ex.SourceFileName = this.templateFile;
            }
        }
Exemplo n.º 5
0
        private void ParseGlobalEnd(string line)
        {
            if (!this.isInGlobalBlock)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.GlobalDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.isInGlobalBlock = false;
        }
Exemplo n.º 6
0
        private void ParseSectionEnd()
        {
            if (!(this.currentDirective is SectionDefinition))
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.SectionDefDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;
        }
Exemplo n.º 7
0
        private void ParseOutputEnd()
        {
            if (this.outputStack.Count == 0)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.OutputDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;

            this.outputStack.Pop();
            this.contextStack.Pop();
        }
Exemplo n.º 8
0
        private void ParseTextEnd()
        {
            if (this.contextStack.Count <= 1 ||
                this.contextStack.Peek().Mode != TemplateMode.Static)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.TextDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;

            this.contextStack.Pop();
        }
Exemplo n.º 9
0
        private void ParseBetweenEnd(string line)
        {
            if (this.contextStack.Count <= 1 ||
                this.contextStack.Peek().Mode != TemplateMode.Dynamic)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(Resources.BetweenDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;

            this.contextStack.Pop();

            this.ParseTextLine(
                line.Substring(
                    line.IndexOf(BetweenEndDirective) + BetweenEndDirective.Length));
        }
Exemplo n.º 10
0
        private void BuildAst()
        {
            string line;

            while ((line = this.reader.ReadLine()) != null)
            {
                this.lineCount++;
                ParseLine(line);
            }

            if (this.isInGlobalBlock)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.GlobalDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }
        }
Exemplo n.º 11
0
        private void ParseImport(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (line.Length <= ImportDirective.Length + 1 ||
                line[ImportDirective.Length] != ' ')
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.ImportDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            string import = line.Substring(ImportDirective.Length + 1);

            if (!this.ast.Head.Imports.Exists(i => i.Value == import))
            {
                this.ast.Head.Imports.Add(new Import(import));
            }
        }
Exemplo n.º 12
0
        private string HandleContextLeadingSpaces(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            string spaces = this.contextStack.Peek().Spaces;

            if (spaces.Length > 0)
            {
                if (!line.StartsWith(spaces))
                {
                    TemplateParsingException ex =
                        new TemplateParsingException(
                            Resources.TextDirectiveSpaceWrong);
                    FillParseError(ex);
                    throw ex;
                }

                line = line.Substring(spaces.Length);
            }

            return(line);
        }
Exemplo n.º 13
0
        private void ParseReference(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (line.Length <= ReferenceDirective.Length + 1 ||
                line[ReferenceDirective.Length] != ' ')
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.ReferenceDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            string reference = line.Substring(ReferenceDirective.Length + 1);

            if (string.IsNullOrEmpty(this.templateFile))
            {
                reference = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    reference);
            }
            else
            {
                reference = Path.Combine(
                    Path.GetDirectoryName(this.templateFile),
                    reference);
            }

            if (!File.Exists(reference))
            {
                reference = Path.GetFileName(reference);
            }

            if (!this.ast.Head.References.Exists(r => r.Value == reference))
            {
                this.ast.Head.References.Add(new Reference(reference));
            }
        }
Exemplo n.º 14
0
        private void MatchEvaluationDirective(
            string line, List<IDirectiveParser> parsers)
        {
            //// @(text)

            Assertion.Assert(line != null);
            Assertion.Assert(parsers != null);

            int sharpCount = 0;

            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == '@')
                {
                    sharpCount++;
                }

                if (line[i] == '@' &&
                    i + 1 < line.Length &&
                    line[i + 1] == '(' &&
                    sharpCount % 2 == 1)
                {
                    int index = i;
                    int lparen = 0;
                    bool matched = false;

                    for (i = i + 2; i < line.Length; i++)
                    {
                        if (line[i] == '(')
                        {
                            lparen++;
                        }
                        else if (line[i] == ')')
                        {
                            if (lparen > 0)
                            {
                                lparen--;
                            }
                            else
                            {
                                int length = i + 1 - index;
                                parsers.Add(
                                    new EvaluationDirectiveParser(
                                        index,
                                        length,
                                        line.Substring(index + 2, length - 3),
                                        this.lineCount,
                                        this));
                                matched = true;

                                sharpCount = 0;
                                break;
                            }
                        }
                    }

                    if (!matched)
                    {
                        TemplateParsingException ex =
                            new TemplateParsingException(
                                Resources.WrongEvaluationDirective);
                        FillParseError(ex);

                        throw ex;
                    }
                }
            }
        }
Exemplo n.º 15
0
        private void ParseSectionStart(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            string trimedLine = line.TrimStart();
            string sectionDef = trimedLine.Substring(SectionDefStartDirective.Length).Trim();

            if (trimedLine.Length == SectionDefStartDirective.Length ||
                trimedLine[SectionDefStartDirective.Length] != ' ' ||
                sectionDef.Length == 0)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.BadSectionDef);
                FillParseError(ex);
                throw ex;
            }

            if (!(this.currentDirective is Body))
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.SectionDefNotTopLevel);
                FillParseError(ex);
                throw ex;
            }

            Match match = sectionExp.Match(sectionDef);

            if (!match.Success)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.BadSectionDef);
                FillParseError(ex);
                throw ex;
            }

            if (this.sections.ContainsKey(match.Groups["name"].Value))
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        String.Format(
                            Resources.SectionNameNotUnique,
                            match.Groups["name"].Value));
                FillParseError(ex);
                throw ex;
            }
            else
            {
                this.sections.Add(match.Groups["name"].Value, null);
            }

            SectionDefinition def = new SectionDefinition(match.Groups["name"].Value);

            for (int i = 0; i < match.Groups["pname"].Captures.Count; i++)
            {
                string pname = match.Groups["pname"].Captures[i].Value;
                string ptype = match.Groups["ptype"].Captures[i].Value;
                def.Parameters.Add(new KeyValuePair <string, string>(pname, ptype));
            }

            this.currentDirective.Directives.Add(def);
            this.currentDirective = def;
        }
Exemplo n.º 16
0
        private void ParseBetweenEnd(string line)
        {
            if (this.contextStack.Count <= 1 ||
                this.contextStack.Peek().Mode != TemplateMode.Dynamic)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(Resources.BetweenDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;

            this.contextStack.Pop();

            this.ParseTextLine(
                line.Substring(
                    line.IndexOf(BetweenEndDirective) + BetweenEndDirective.Length));
        }
Exemplo n.º 17
0
        private void ParseGlobalEnd(string line)
        {
            if (!this.isInGlobalBlock)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.GlobalDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.isInGlobalBlock = false;
        }
Exemplo n.º 18
0
        private void ParseImport(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (line.Length <= ImportDirective.Length + 1 ||
                line[ImportDirective.Length] != ' ')
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.ImportDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            string import = line.Substring(ImportDirective.Length + 1);

            if (!this.ast.Head.Imports.Exists(i => i.Value == import))
            {
                this.ast.Head.Imports.Add(new Import(import));
            }
        }
Exemplo n.º 19
0
        private void ParseOutputEnd()
        {
            if (this.outputStack.Count == 0)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.OutputDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;

            this.outputStack.Pop();
            this.contextStack.Pop();
        }
Exemplo n.º 20
0
        private void ParseSectionStart(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            string trimedLine = line.TrimStart();
            string sectionDef = trimedLine.Substring(SectionDefStartDirective.Length).Trim();

            if (trimedLine.Length == SectionDefStartDirective.Length ||
                trimedLine[SectionDefStartDirective.Length] != ' ' ||
                sectionDef.Length == 0)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.BadSectionDef);
                FillParseError(ex);
                throw ex;
            }

            if (!(this.currentDirective is Body))
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.SectionDefNotTopLevel);
                FillParseError(ex);
                throw ex;
            }

            Match match = sectionExp.Match(sectionDef);

            if (!match.Success)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.BadSectionDef);
                FillParseError(ex);
                throw ex;
            }

            if (this.sections.ContainsKey(match.Groups["name"].Value))
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        String.Format(
                            Resources.SectionNameNotUnique,
                            match.Groups["name"].Value));
                FillParseError(ex);
                throw ex;
            }
            else
            {
                this.sections.Add(match.Groups["name"].Value, null);
            }

            SectionDefinition def = new SectionDefinition(match.Groups["name"].Value);

            for (int i = 0; i < match.Groups["pname"].Captures.Count; i++)
            {
                string pname = match.Groups["pname"].Captures[i].Value;
                string ptype = match.Groups["ptype"].Captures[i].Value;
                def.Parameters.Add(new KeyValuePair<string, string>(pname, ptype));
            }

            this.currentDirective.Directives.Add(def);
            this.currentDirective = def;
        }
Exemplo n.º 21
0
        private void ParseParam(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (line.Length <= ParamDirective.Length + 1 ||
                line[ParamDirective.Length] != ' ')
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.ParamDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            Match m = AtParser.paramExpr.Match(line, ParamDirective.Length);

            if (!m.Success)
            {
                TemplateParsingException ex = new TemplateParsingException(
                    Resources.ParamDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            Parameter p = new Parameter(
                m.Groups["paramName"].Value,
                m.Groups["paramType"].Value.TrimEnd());
            this.ast.Head.Parameters.Add(p);

            if (this.debug)
            {
                p.Line = this.lineCount;
            }
        }
Exemplo n.º 22
0
        private void MatchEvaluationDirective(
            string line, List <IDirectiveParser> parsers)
        {
            //// @(text)

            Assertion.Assert(line != null);
            Assertion.Assert(parsers != null);

            int sharpCount = 0;

            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == '@')
                {
                    sharpCount++;
                }

                if (line[i] == '@' &&
                    i + 1 < line.Length &&
                    line[i + 1] == '(' &&
                    sharpCount % 2 == 1)
                {
                    int  index   = i;
                    int  lparen  = 0;
                    bool matched = false;

                    for (i = i + 2; i < line.Length; i++)
                    {
                        if (line[i] == '(')
                        {
                            lparen++;
                        }
                        else if (line[i] == ')')
                        {
                            if (lparen > 0)
                            {
                                lparen--;
                            }
                            else
                            {
                                int length = i + 1 - index;
                                parsers.Add(
                                    new EvaluationDirectiveParser(
                                        index,
                                        length,
                                        line.Substring(index + 2, length - 3),
                                        this.lineCount,
                                        this));
                                matched = true;

                                sharpCount = 0;
                                break;
                            }
                        }
                    }

                    if (!matched)
                    {
                        TemplateParsingException ex =
                            new TemplateParsingException(
                                Resources.WrongEvaluationDirective);
                        FillParseError(ex);

                        throw ex;
                    }
                }
            }
        }
Exemplo n.º 23
0
        private void ParseReference(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (line.Length <= ReferenceDirective.Length + 1 ||
                line[ReferenceDirective.Length] != ' ')
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.ReferenceDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            string reference = line.Substring(ReferenceDirective.Length + 1);

            if (string.IsNullOrEmpty(this.templateFile))
            {
                reference = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    reference);
            }
            else
            {
                reference = Path.Combine(
                    Path.GetDirectoryName(this.templateFile),
                    reference);
            }

            if (!File.Exists(reference))
            {
                reference = Path.GetFileName(reference);
            }

            if (!this.ast.Head.References.Exists(r => r.Value == reference))
            {
                this.ast.Head.References.Add(new Reference(reference));
            }
        }
Exemplo n.º 24
0
        private void ParseSectionEnd()
        {
            if (!(this.currentDirective is SectionDefinition))
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.SectionDefDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;
        }
Exemplo n.º 25
0
        private void FillParseError(TemplateParsingException ex)
        {
            Assertion.Assert(ex != null, "ex cannot be null.");

            ex.SourceLine = this.lineCount;

            if (this.templateFile != null)
            {
                ex.SourceFileName = this.templateFile;
            }
        }
Exemplo n.º 26
0
        private void ParseTextEnd()
        {
            if (this.contextStack.Count <= 1 ||
                this.contextStack.Peek().Mode != TemplateMode.Static)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.TextDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective = this.currentDirective.Parent;

            this.contextStack.Pop();
        }
Exemplo n.º 27
0
        private string HandleOutputLeadingSpaces(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            if (this.outputStack.Count > 0)
            {
                string spaces = this.outputStack.Peek().Spaces;

                if (spaces.Length > 0)
                {
                    if (!line.StartsWith(spaces))
                    {
                        TemplateParsingException ex =
                            new TemplateParsingException(Resources.OutputDirectiveSpaceWrong);
                        FillParseError(ex);
                        throw ex;
                    }

                    line = line.Substring(spaces.Length);
                }
            }

            return line;
        }
Exemplo n.º 28
0
        private void ParseOutputStart(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            string trimedLine = line.TrimStart();
            string key = trimedLine.Substring(OutputStartDirective.Length).Trim();

            if (trimedLine.Length == OutputStartDirective.Length ||
                trimedLine[OutputStartDirective.Length] != ' ' ||
                key.Length == 0)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.NoOutputKeyProvided);
                FillParseError(ex);
                throw ex;
            }

            Output output = new Output(key);
            this.currentDirective.Directives.Add(output);
            this.currentDirective = output;

            this.outputStack.Push(
                new OutputContext(
                    key,
                    line.Substring(0, CountLeadingWhiteSpace(line))));

            this.contextStack.Push(new Context(TemplateMode.Static, ""));
        }
Exemplo n.º 29
0
        private void BuildAst()
        {
            string line;

            while ((line = this.reader.ReadLine()) != null)
            {
                this.lineCount++;
                ParseLine(line);
            }

            if (this.isInGlobalBlock)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.GlobalDirectiveWrong);
                FillParseError(ex);
                throw ex;
            }
        }
Exemplo n.º 30
0
        private void ParseSectionRef(string line)
        {
            Assertion.Assert(line != null, "line cannot be null.");

            int index = line.IndexOf(SectionRefDirective);
            string spaces = line.Substring(0, index);
            string text = line.Substring(
                index + SectionRefDirective.Length);

            Match match = nameExp.Match(text);

            if (!match.Success)
            {
                TemplateParsingException ex =
                    new TemplateParsingException(
                        Resources.BadSectionRef);
                FillParseError(ex);
                throw ex;
            }

            this.currentDirective.Directives.Add(
                new SectionReference(
                    match.Groups["name"].Value,
                    match.Groups["rest"].Value.Trim(),
                    spaces,
                    this.lineCount));
        }