Skip to content

Commit

Permalink
Refactor and add new Parser tests.
Browse files Browse the repository at this point in the history
This also fixes some edge cases in the parser that these tests caught.

Issue #8
  • Loading branch information
TheModMaker committed Mar 9, 2021
1 parent fa76f34 commit 4dd1e67
Show file tree
Hide file tree
Showing 2 changed files with 822 additions and 730 deletions.
42 changes: 22 additions & 20 deletions ModMaker.Lua/Parser/PlainParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,10 @@ protected virtual IParseStatement _readClass(Lexer input) {
if (input.PeekType(TokenType.StringLiteral)) {
className = input.Expect(TokenType.StringLiteral).Value;
if (input.ReadIfType(TokenType.BeginParen)) {
while (!input.PeekType(TokenType.EndParen)) {
do {
Token name = input.Expect(TokenType.Identifier);
implements.Add(name.Value);
if (!input.ReadIfType(TokenType.Comma)) {
break;
}
}
} while (input.ReadIfType(TokenType.Comma));
input.Expect(TokenType.EndParen);
}
} else {
Expand Down Expand Up @@ -206,7 +203,8 @@ protected virtual ReturnItem _readReturn(Lexer input) {

bool isParentheses = false;
if (!input.PeekType(TokenType.End) && !input.PeekType(TokenType.Until) &&
!input.PeekType(TokenType.ElseIf) && !input.PeekType(TokenType.Else)) {
!input.PeekType(TokenType.ElseIf) && !input.PeekType(TokenType.Else) &&
!input.PeekType(TokenType.None)) {
values.Add(_readExp(input, out isParentheses));
while (input.ReadIfType(TokenType.Comma)) {
values.Add(_readExp(input, out isParentheses));
Expand Down Expand Up @@ -388,9 +386,10 @@ protected virtual AssignmentItem _readAssignment(Lexer input, Token debug, bool
IParseVariable variable) {
var names = new List<IParseVariable>() { variable };
while (input.ReadIfType(TokenType.Comma)) {
var curDebug = input.Peek();
var exp = _readExp(input, out _);
if ((local && !(exp is NameItem)) || (!local && !(exp is IParseVariable))) {
throw new SyntaxException(Resources.NameOrExpForVar, input.Name, debug);
throw new SyntaxException(Resources.NameOrExpForVar, input.Name, curDebug);
}
names.Add((IParseVariable)exp);
}
Expand All @@ -404,8 +403,7 @@ protected virtual AssignmentItem _readAssignment(Lexer input, Token debug, bool
exps.Add(_readExp(input, out isParentheses));
}
} else if (!local) {
throw new SyntaxException(string.Format(Resources.InvalidDefinition, "assignment"),
input.Name, debug);
throw input.SyntaxError(string.Format(Resources.InvalidDefinition, "assignment"));
}

return new AssignmentItem(names.ToArray(), exps.ToArray()) {
Expand Down Expand Up @@ -453,15 +451,15 @@ protected virtual IParseExp _readPrefixExp(Lexer input, out bool isParentheses)
if (!int.TryParse(instName.Substring(idx + 1), out overload)) {
throw input.SyntaxError(Resources.OnlyNumbersInOverload);
}
instName = instName.Substring(0, idx - 1);
instName = instName.Substring(0, idx);
}
} else if (ret is NameItem name) {
int idx = name.Name.IndexOf('`');
if (idx >= 0) {
if (!int.TryParse(name.Name.Substring(idx + 1), out overload)) {
throw input.SyntaxError(Resources.OnlyNumbersInOverload);
}
name.Name = name.Name.Substring(0, idx - 1);
name.Name = name.Name.Substring(0, idx);
}
}

Expand Down Expand Up @@ -592,18 +590,22 @@ protected virtual FuncDefItem _readFunctionHelper(Lexer input, bool canName, boo
if (name != null && !canName) {
throw new SyntaxException(Resources.FunctionCantHaveName, input.Name, debug);
}
if (name == null && canName) {
throw new SyntaxException("Function statements must provide name", input.Name, debug);
}

var args = new List<NameItem>();
input.Expect(TokenType.BeginParen);
while (!input.PeekType(TokenType.EndParen)) {
Token temp = input.PeekType(TokenType.Elipsis) ?
input.Expect(TokenType.Elipsis) :
input.Expect(TokenType.Identifier);
args.Add(new NameItem(temp.Value) { Debug = temp });

if (!input.PeekType(TokenType.EndParen)) {
input.Expect(TokenType.Comma);
}
if (!input.PeekType(TokenType.EndParen)) {
do {
Token temp = input.PeekType(TokenType.Elipsis) ?
input.Expect(TokenType.Elipsis) :
input.Expect(TokenType.Identifier);
args.Add(new NameItem(temp.Value) { Debug = temp });
if (temp.Value == "...") {
break;
}
} while (input.ReadIfType(TokenType.Comma));
}
input.Expect(TokenType.EndParen);
BlockItem chunk = _readBlock(input);
Expand Down
Loading

0 comments on commit 4dd1e67

Please sign in to comment.