TableField ParseTableIndexField() { NextToken(); var field = new TableField(_current.m_line); field.index = ParseExp(); if (NextToken().m_type != ']') { throw NewParserException("expect ']'", _current); } if (NextToken().m_type != '=') { throw NewParserException("expect '='", _current); } field.value = ParseExp(); return(field); }
TableDefine ParseTableConstructor() { NextToken(); var table = new TableDefine(_current.m_line); TableField last_field = null; while (LookAhead().m_type != '}') { if (LookAhead().m_type == (int)'[') { last_field = ParseTableIndexField(); } else { last_field = new TableField(LookAhead().m_line); if (LookAhead().Match(TokenType.STRING_BEGIN)) { var exp = ParseComplexString(); if (LookAhead().Match('=')) { last_field.index = exp; NextToken(); last_field.value = ParseExp(); } else { last_field.value = exp; } } else if (LookAhead2().Match('=')) { // must be kv NextToken(); if (_current.Match(TokenType.STRING) || _current.Match(TokenType.NUMBER)) { last_field.index = new Terminator(_current); } else if (_current.IsLiteralString()) { last_field.index = new Terminator(_current.ConvertToStringToken()); } else { throw NewParserException("expect <name>,<string>,<number> to define table-key before '='", _current); } NextToken();// skip = last_field.value = ParseExp(); } else { last_field.value = ParseExp(); } } table.fields.Add(last_field); if (LookAhead().m_type != '}') { NextToken(); if (_current.m_type != (int)',' && _current.m_type != (int)';') { throw NewParserException("expect ',' or ';' to split table fields", _current); } } } if (NextToken().m_type != '}') { throw NewParserException("expect '}' for table", _current); } return(table); }