示例#1
0
 private void StartNode(HmlToken name)
 {
     this.nodeName       = name.Content;
     this.position       = name.Position;
     this.nodeText       = null;
     this.nodeProperties = null;
     this.isNodeStarted  = true;
 }
示例#2
0
 public HmlParsingException(HmlToken token, string message) : base(message)
 {
     this.Token = token;
 }
示例#3
0
        private void ParseNode(HmlToken token)
        {
            if (token.Type == HmlTokenType.EndOfDocument)
            {
                if (isNodeStarted)
                {
                    this.CreateNode();
                }
                return;
            }

            // Ignoring Whitespaces if not at the beginning of document or line
            if (token.Type == HmlTokenType.Whitespaces)
            {
                if (token.Position.Start == 0 || lastToken?.Type == HmlTokenType.LineReturn)
                {
                    this.tokens.Add(lastToken = token);
                    indent = token.Content.Length;
                }
            }
            else
            {
                if (lastToken == null)
                {
                    if (token.Type == HmlTokenType.Identifier)
                    {
                        this.StartNode(token);
                    }
                    else if (token.Type != HmlTokenType.LineReturn)
                    {
                        // Error : expected <node identifier> or indent
                        this.Throw(token, HmlTokenType.Identifier, HmlTokenType.Whitespaces);
                    }
                }
                else if (lastToken.Type == HmlTokenType.Whitespaces)
                {
                    if (token.Type == HmlTokenType.Identifier)
                    {
                        this.StartNode(token);
                    }
                    else
                    {
                        // Error : expected <node identifier>
                        this.Throw(token, HmlTokenType.Identifier);
                    }
                }
                else if (lastToken.Type == HmlTokenType.Identifier)
                {
                    if (!arePropertiesStarted)
                    {
                        if (token.Type == HmlTokenType.PropertiesStart)
                        {
                            this.StartProperties();
                        }
                        else if (token.Type == HmlTokenType.Text)
                        {
                            nodeText = token.Content;
                        }
                        else if (token.Type == HmlTokenType.LineReturn)
                        {
                            this.CreateNode();
                        }
                        else
                        {
                            // Error : expected '(' or ':' or <line return>
                            this.Throw(token, HmlTokenType.PropertiesStart, HmlTokenType.Text, HmlTokenType.LineReturn);
                        }
                    }
                    else
                    {
                        if (token.Type == HmlTokenType.Equals)
                        {
                            propertyName = lastToken.Content;
                        }
                        else
                        {
                            // Error : expected '='
                            this.Throw(token, HmlTokenType.Equals);
                        }
                    }
                }
                else if (lastToken.Type == HmlTokenType.Equals)
                {
                    if (token.Type == HmlTokenType.PropertyValue)
                    {
                        this.nodeProperties[propertyName] = token.Content;
                        this.propertyName = null;
                    }
                    else
                    {
                        // Error : expected '"<property value>"'
                        this.Throw(token, HmlTokenType.PropertyValue);
                    }
                }
                else if (lastToken.Type == HmlTokenType.PropertyValue)
                {
                    if (token.Type == HmlTokenType.PropertiesEnd)
                    {
                        this.EndProperties();
                    }
                    else if (token.Type != HmlTokenType.PropertiesSeparator)
                    {
                        // Error : expected ',' or `)`
                        this.Throw(token, HmlTokenType.PropertiesSeparator, HmlTokenType.PropertiesEnd);
                    }
                }
                else if (lastToken.Type == HmlTokenType.PropertiesSeparator)
                {
                    if (token.Type == HmlTokenType.Identifier)
                    {
                        propertyName = token.Content;
                    }
                    else
                    {
                        // Error : expected <identifier>
                        this.Throw(token, HmlTokenType.Identifier);
                    }
                }
                else if (lastToken.Type == HmlTokenType.PropertiesStart)
                {
                    if (token.Type == HmlTokenType.Identifier)
                    {
                        propertyName = token.Content;
                    }
                    else if (token.Type == HmlTokenType.PropertiesEnd)
                    {
                        this.EndProperties();
                    }
                    else
                    {
                        // Error : expected <property identifier> or ')'
                        this.Throw(token, HmlTokenType.Identifier, HmlTokenType.PropertiesEnd);
                    }
                }
                else if (lastToken.Type == HmlTokenType.PropertiesEnd)
                {
                    if (token.Type == HmlTokenType.Text)
                    {
                        this.nodeText = token.Content;
                    }
                    else if (token.Type == HmlTokenType.LineReturn)
                    {
                        this.CreateNode();
                    }
                    else
                    {
                        // Error : expected ':<text>' or '\n'
                        this.Throw(token, HmlTokenType.Text, HmlTokenType.LineReturn);
                    }
                }
                else if (lastToken.Type == HmlTokenType.Text)
                {
                    if (token.Type == HmlTokenType.LineReturn)
                    {
                        this.CreateNode();
                    }
                    else
                    {
                        // Error : expected '\n'
                        this.Throw(token, HmlTokenType.LineReturn);
                    }
                }
                else if (lastToken.Type == HmlTokenType.LineReturn)
                {
                    if (token.Type == HmlTokenType.Identifier)
                    {
                        this.StartNode(token);
                    }
                    else if (token.Type != HmlTokenType.Whitespaces && token.Type != HmlTokenType.LineReturn)
                    {
                        // Error : expected '<node identifier>' or ' ' or '\n'
                        this.Throw(token, HmlTokenType.Identifier, HmlTokenType.Whitespaces, HmlTokenType.LineReturn);
                    }
                }

                this.tokens.Add(lastToken = token);
            }
        }
示例#4
0
 private void Throw(HmlToken token, params HmlTokenType[] expected)
 {
     throw new HmlInvalidTokenParsingException(token, this.lastToken, expected);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Hml.Parser.Exceptions.HmlInvalidTokenParsingException"/> class.
 /// </summary>
 /// <param name="token">Token.</param>
 /// <param name="previousToken">Previous token.</param>
 /// <param name="expected">Expected.</param>
 public HmlInvalidTokenParsingException(HmlToken token, HmlToken previousToken, params HmlTokenType[] expected) : base(token, $"got token {GetTokenInfo(token.Type)} at position [{token.Position.Line}, {token.Position.Column}] (following { (previousToken != null ? GetTokenInfo(previousToken.Type) : "start")}), expected : { string.Join(", ", expected.Select(x => GetTokenInfo(x))) }")
 {
     this.PreviousToken  = previousToken;
     this.ExpectedTokens = expected;
 }