add: Variable declarations test.
This commit is contained in:
parent
eedbd5e7e5
commit
c7077bd0fd
|
@ -9,7 +9,9 @@ namespace CanonSharp.Pascal.Parser;
|
||||||
public abstract class GrammarParserBuilder
|
public abstract class GrammarParserBuilder
|
||||||
{
|
{
|
||||||
protected static IParser<LexicalToken, LexicalToken> Keyword(string value)
|
protected static IParser<LexicalToken, LexicalToken> Keyword(string value)
|
||||||
=> Satisfy<LexicalToken>(token => token.TokenType == LexicalTokenType.Keyword && token.LiteralValue == value);
|
=> Satisfy<LexicalToken>(token =>
|
||||||
|
token.TokenType == LexicalTokenType.Keyword &&
|
||||||
|
token.LiteralValue.Equals(value, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
protected static IParser<LexicalToken, LexicalToken> Operator(string value)
|
protected static IParser<LexicalToken, LexicalToken> Operator(string value)
|
||||||
=> Satisfy<LexicalToken>(token => token.TokenType == LexicalTokenType.Operator && token.LiteralValue == value);
|
=> Satisfy<LexicalToken>(token => token.TokenType == LexicalTokenType.Operator && token.LiteralValue == value);
|
||||||
|
|
|
@ -66,4 +66,66 @@ public class ProgramParserTests : GrammarParserTestBase
|
||||||
Assert.Equal(1, binaryOperatorNode.Left.Convert<IntegerValueNode>().Value);
|
Assert.Equal(1, binaryOperatorNode.Left.Convert<IntegerValueNode>().Value);
|
||||||
Assert.Equal(1, binaryOperatorNode.Right.Convert<IntegerValueNode>().Value);
|
Assert.Equal(1, binaryOperatorNode.Right.Convert<IntegerValueNode>().Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ProgramParseTest4()
|
||||||
|
{
|
||||||
|
Program program = ProgramParse("""
|
||||||
|
program main;
|
||||||
|
var a : integer;
|
||||||
|
begin
|
||||||
|
a := 1 + 1;
|
||||||
|
end.
|
||||||
|
""");
|
||||||
|
|
||||||
|
IList<VariableDeclarationNode> variableDeclarationNodes = program.Body.VariableDeclarations.Statements
|
||||||
|
.Select(x => x.Convert<VariableDeclarationNode>()).ToList();
|
||||||
|
Assert.Single(variableDeclarationNodes);
|
||||||
|
Assert.Single(variableDeclarationNodes.First().Identifiers);
|
||||||
|
Assert.Equal("a", variableDeclarationNodes.First().Identifiers.First().LiteralValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("""
|
||||||
|
program DoNothing;
|
||||||
|
begin
|
||||||
|
end.
|
||||||
|
""")]
|
||||||
|
[InlineData("""
|
||||||
|
program Add;
|
||||||
|
var a : Integer;
|
||||||
|
begin
|
||||||
|
a := 1 + 1
|
||||||
|
end.
|
||||||
|
""")]
|
||||||
|
[InlineData("""
|
||||||
|
program varTest;
|
||||||
|
var a : integer;
|
||||||
|
begin
|
||||||
|
a := 9 div 1;
|
||||||
|
end.
|
||||||
|
""")]
|
||||||
|
[InlineData("""
|
||||||
|
program main;
|
||||||
|
var a : integer;
|
||||||
|
begin
|
||||||
|
a := 1 + +1;
|
||||||
|
a := 1 - -1;
|
||||||
|
a := 1 + -1;
|
||||||
|
a := 1 - +1;
|
||||||
|
end.
|
||||||
|
""")]
|
||||||
|
[InlineData("""
|
||||||
|
program main;
|
||||||
|
const a = true; b = false;
|
||||||
|
var c, d : boolean;
|
||||||
|
begin
|
||||||
|
c := true;
|
||||||
|
d := false;
|
||||||
|
end.
|
||||||
|
""")]
|
||||||
|
public void ProgramParseTest(string input)
|
||||||
|
{
|
||||||
|
ProgramParse(input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,5 +16,34 @@ public class VariableDeclarationTests : GrammarParserTestBase
|
||||||
TypeNode node = RunParser<TypeNode>(GrammarParser.TypeParser(), input);
|
TypeNode node = RunParser<TypeNode>(GrammarParser.TypeParser(), input);
|
||||||
Assert.Equal(value, node.TypeToken.LiteralValue);
|
Assert.Equal(value, node.TypeToken.LiteralValue);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void VariableDeclarationTest()
|
||||||
|
{
|
||||||
|
VariableDeclarationNode node = RunParser<VariableDeclarationNode>(GrammarParser.VariableDeclarationParser(),
|
||||||
|
"a : integer");
|
||||||
|
|
||||||
|
Assert.Contains(node.Identifiers, token => token.LiteralValue == "a");
|
||||||
|
Assert.Equal("integer", node.TypeNode.TypeToken.LiteralValue);
|
||||||
|
|
||||||
|
node = RunParser<VariableDeclarationNode>(GrammarParser.VariableDeclarationParser(),
|
||||||
|
"a, b, c : boolean");
|
||||||
|
|
||||||
|
Assert.Contains(node.Identifiers, token => token.LiteralValue == "a");
|
||||||
|
Assert.Contains(node.Identifiers, token => token.LiteralValue == "b");
|
||||||
|
Assert.Contains(node.Identifiers, token => token.LiteralValue == "c");
|
||||||
|
Assert.Equal("boolean", node.TypeNode.TypeToken.LiteralValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("var a : integer;", 1)]
|
||||||
|
[InlineData("var a : integer; b : boolean;", 2)]
|
||||||
|
[InlineData("var a : integer; b,c : boolean;" +
|
||||||
|
"d : char", 3)]
|
||||||
|
[InlineData("var a,d,e : integer; b : boolean; c: real;", 3)]
|
||||||
|
public void VariableDeclarationsTest(string input, int count)
|
||||||
|
{
|
||||||
|
BlockNode node = RunParser<BlockNode>(GrammarParser.VariableDeclarationsParser(), input);
|
||||||
|
Assert.Equal(count, node.Statements.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user