diff options
| author | dibyendumajumdar <mobile@majumdar.org.uk> | 2023-08-21 18:00:18 +0100 |
|---|---|---|
| committer | dibyendumajumdar <mobile@majumdar.org.uk> | 2023-08-21 18:00:18 +0100 |
| commit | b6098a9b9166da962676e33de31e93ace260d686 (patch) | |
| tree | 9ca61794cda0f042efba1e6832353950cb1b3a57 /src/test/java/com/seaofnodes | |
| parent | c0250dbac620448ec4909615e9c571eece6be08b (diff) | |
| download | Simple-b6098a9b9166da962676e33de31e93ace260d686.tar.gz Simple-b6098a9b9166da962676e33de31e93ace260d686.zip | |
Implement parser for a simple language that roughly corresponds to appendix A in Aho's 2nd edition
Diffstat (limited to 'src/test/java/com/seaofnodes')
| -rw-r--r-- | src/test/java/com/seaofnodes/simple/lexer/TestLexer.java | 14 | ||||
| -rw-r--r-- | src/test/java/com/seaofnodes/simple/lexer/TestParser.java | 68 |
2 files changed, 77 insertions, 5 deletions
diff --git a/src/test/java/com/seaofnodes/simple/lexer/TestLexer.java b/src/test/java/com/seaofnodes/simple/lexer/TestLexer.java index 335bcf5e..18a4634d 100644 --- a/src/test/java/com/seaofnodes/simple/lexer/TestLexer.java +++ b/src/test/java/com/seaofnodes/simple/lexer/TestLexer.java @@ -3,8 +3,6 @@ * * Contributor(s): * - * The Initial Developer of the Original Software is Cliff Click. - * * This file is part of the Sea Of Nodes Simple Programming language * implementation. See https://github.com/SeaOfNodes/Simple * @@ -17,6 +15,10 @@ package com.seaofnodes.simple.lexer; +import com.seaofnodes.simple.common.ErrorListener; +import com.seaofnodes.simple.lexer.ecstasy.Lexer; +import com.seaofnodes.simple.lexer.ecstasy.Source; +import com.seaofnodes.simple.lexer.ecstasy.Token; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -26,8 +28,10 @@ public class TestLexer { public void testLexLineComment() { final String source = "//a comment\nx"; - Lexer lexer = new Lexer("test", source); - String token = lexer.token(); - Assertions.assertEquals("x", token); + Lexer lexer = new Lexer(new Source(source), new ErrorListener()); + Token token = lexer.next(); + Assertions.assertEquals(token.getId(), Token.Id.EOL_COMMENT); + token = lexer.next(); + Assertions.assertEquals("x", token.getValueText()); } } diff --git a/src/test/java/com/seaofnodes/simple/lexer/TestParser.java b/src/test/java/com/seaofnodes/simple/lexer/TestParser.java new file mode 100644 index 00000000..7d2f3752 --- /dev/null +++ b/src/test/java/com/seaofnodes/simple/lexer/TestParser.java @@ -0,0 +1,68 @@ +/** + * DO NOT REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Contributor(s): + * + * This file is part of the Sea Of Nodes Simple Programming language + * implementation. See https://github.com/SeaOfNodes/Simple + * + * The contents of this file are subject to the terms of the + * Apache License Version 2 (the "APL"). You may not use this + * file except in compliance with the License. A copy of the + * APL may be obtained from: + * http://www.apache.org/licenses/LICENSE-2.0 + */ + +package com.seaofnodes.simple.lexer; + +import com.seaofnodes.simple.common.ErrorListener; +import com.seaofnodes.simple.lexer.ecstasy.Lexer; +import com.seaofnodes.simple.lexer.ecstasy.Source; +import com.seaofnodes.simple.parser.Ast; +import com.seaofnodes.simple.parser.Parser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestParser { + + @Test + public void testParseSingleDecl() + { + final String source = "var i: Int;"; + Lexer lexer = new Lexer(new Source(source), new ErrorListener()); + Parser parser = new Parser(lexer); + var ast = parser.parse(); + Assertions.assertTrue(ast instanceof Ast.Block + && ((Ast.Block) ast).stmtList.size() == 1); + } + + @Test + public void testParseTwoDecls() { + final String source = "var i: Int;\nvar j: Int;"; + Lexer lexer = new Lexer(new Source(source), new ErrorListener()); + Parser parser = new Parser(lexer); + var ast = parser.parse(); + Assertions.assertTrue(ast instanceof Ast.Block + && ((Ast.Block) ast).stmtList.size() == 2); + } + + @Test + public void testIfAndWhile() { + final String source = + """ + var i: Int; + var j: Int; + + i = 0; + j = 100; + while ( 1 ) { + i = i + 1; + j = j - 1; + if ( i >= j ) break; + } + """; + Lexer lexer = new Lexer(new Source(source), new ErrorListener()); + Parser parser = new Parser(lexer); + var ast = parser.parse(); + } +} |
