Esempio n. 1
0
        Ast.Entry GetEntryOrJunk(FtlParserStream ps)
        {
            var entryStartPos = ps.GetPosition();

            ps.BeginCapture();

            try
            {
                var entry = GetEntry(ps);
                ps.ExpectNewLine();
                return(entry);
            }
            catch (ParseException e)
            {
                var errorPos = ps.GetPosition();
                ps.SkipToNextEntryStart();
                var nextEntryStart = ps.GetPosition();

                // Create a Junk instance
                var junk = new Ast.Junk(ps.GetCapturedText());
                if (_withSpans)
                {
                    junk.AddSpan(entryStartPos, nextEntryStart);
                }
                var annot = new Ast.Annotation(e.Code, e.Args, e.Message);
                annot.AddSpan(errorPos, errorPos);
                junk.AddAnnotation(annot);
                return(junk);
            }
        }
Esempio n. 2
0
        public Ast.Resource Parse(TextReader input)
        {
            var ps = new FtlParserStream(input);

            ps.SkipBlankLines();

            var entries = new List <Ast.Entry>();

            Ast.Comment lastComment = null;

            while (ps.Current != Eof)
            {
                var entry = GetEntryOrJunk(ps);

                int blankLines = ps.SkipBlankLines();
                // Regular Comments require special logic. Comments may be attached to
                // Messages or Terms if they are followed immediately by them. However
                // they should parse as standalone when they're followed by Junk.
                // Consequently, we only attach Comments once we know that the Message
                // or the Term parsed successfully.
                if (entry is Ast.Comment comment &&
                    blankLines == 0 && ps.Current != Eof)
                {
                    // Stash the comment and decide what to do with it in the next pass.
                    lastComment = comment;
                    continue;
                }

                if (lastComment != null)
                {
                    if (entry is Ast.MessageTermBase mt)
                    {
                        mt.Comment = lastComment;
                        if (_withSpans)
                        {
                            mt.Span.Start = lastComment.Span.Start;
                        }
                    }
                    else
                    {
                        entries.Add(lastComment);
                    }
                    // In either case, the stashed comment has been dealt with; clear it.
                    lastComment = null;
                }

                // No special logic for other types of entries.
                entries.Add(entry);
            }

            var res = new Ast.Resource(entries);

            if (_withSpans)
            {
                res.AddSpan(Position.Start, ps.GetPosition());
            }

            return(res);
        }
Esempio n. 3
0
        T SpanWrapper <T>(FtlParserStream ps, Func <T> wrappedFn) where T : Ast.SyntaxNode
        {
            if (!_withSpans)
            {
                return(wrappedFn());
            }

            var start = ps.GetPosition();
            var node  = wrappedFn();

            // Don't re-add the span if the node already has it.  This may happen when
            // one decorated function calls another decorated function.
            if (node.Span != null)
            {
                return(node);
            }

            var end = ps.GetPosition();

            node.AddSpan(start, end);
            return(node);
        }