Beispiel #1
0
        public IEnumerable<IDocumentPart> Parse (Lexer lexer)
        {
            bool have_parsed_headers = false;

            foreach (var unit in ParseUnits (lexer)) {
                if (have_parsed_headers) {
                    yield return unit;
                    continue;
                }

                var is_header_message = false;
                foreach (var message in unit.Messages) {
                    if (!message.HasValue) {
                        break;
                    } else if (message.Type == MessageType.SingularIdentifier && message.Value == String.Empty) {
                        is_header_message = true;
                    } else if (message.Type == MessageType.SingularString && is_header_message) {
                        var headers = new HeaderCollection ();

                        foreach (var header in ParseHeaders (message)) {
                            have_parsed_headers = true;
                            headers.Add (header);
                        }

                        if (have_parsed_headers) {
                            yield return headers;
                        } else {
                            yield return unit;
                        }

                        break;
                    }
                }
            }
        }
Beispiel #2
0
 public SyntaxException(Lexer lexer, string message)
     : base(String.Format ("Syntax error: \"{0}\": {1}:{2},{3} ('{4}')",
         message, lexer.Path, lexer.Line, lexer.Column, lexer.Peek ()))
 {
 }
Beispiel #3
0
 public SyntaxException(Lexer lexer, string message)
     : base(String.Format("Syntax error: \"{0}\": {1}:{2},{3}",
                          message, lexer.Path, lexer.Line, lexer.Column))
 {
 }
Beispiel #4
0
        private Message ParseIdentifier (Lexer lexer, Token.Identifier identifier)
        {
            var match = Regex.Match ((string)identifier, @"^msg(id|id_plural|str|str\[(\d+)\]|ctxt)$");
            if (!match.Success) {
                throw new SyntaxException (lexer, "invalid identifier: " + (string)identifier);
            }

            int plural_order;
            MessageType type;

            switch (match.Groups [1].Value) {
                case "id":
                    type = MessageType.SingularIdentifier;
                    plural_order = -1;
                    break;
                case "id_plural":
                    type = MessageType.PluralIdentifier;
                    plural_order = -1;
                    break;
                case "str":
                    type = MessageType.SingularString;
                    plural_order = 0;
                    break;
                case "ctxt":
                    type = MessageType.Context;
                    plural_order = -1;
                    break;
                default:
                    if (match.Groups.Count == 3) {
                        type = MessageType.PluralString;
                        plural_order = Int32.Parse (match.Groups [2].Value);
                    } else {
                        throw new SyntaxException(lexer, "invalid identifier: " + (string)identifier);
                    }
                    break;
            }

            return new Message (identifier, type, plural_order);
        }
Beispiel #5
0
        private IEnumerable<Unit> ParseUnits (Lexer lexer)
        {
            var unit = new Unit ();
            Token last_msgstr_token = null;
            Message message = null;

            foreach (var token in lexer.Lex ()) {
                if (IsMsgstrToken (token)) {
                    last_msgstr_token = token;
                } else if (IsStartOfUnitToken (token) && last_msgstr_token != null) {
                    last_msgstr_token = null;
                    yield return unit;
                    unit = new Unit ();
                }

                if (token is Token.Comment) {
                    unit.Add (new Comment ((Token.Comment)token));
                } else if (token is Token.Identifier) {
                    message = ParseIdentifier (lexer, (Token.Identifier)token);
                    unit.Add (message);
                } else if (token is Token.String) {
                    message.Value += (string)token;
                }
            }

            yield return unit;
        }