Skip to content
Snippets Groups Projects
Commit 7ff65a57 authored by Nathan PERIER's avatar Nathan PERIER
Browse files

Change of syntax to get a `TokenPeekIterator`

parent 8eb0a875
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ import fr.insarennes.nperier.minichamo.utils.SymbolPosition;
import fr.insarennes.nperier.minichamo.lexing.tokens.*;
import fr.insarennes.nperier.minichamo.lexing.utils.CharRegex;
import fr.insarennes.nperier.minichamo.lexing.utils.PeekCharIterator;
import fr.insarennes.nperier.minichamo.utils.TokenPeekIterator;
import java.io.BufferedReader;
import java.io.FileReader;
......@@ -41,6 +42,10 @@ public class Lexer implements Iterator<Token> {
return new Lexer(resultStringBuilder.toString());
}
public TokenPeekIterator wrap() {
return new TokenPeekIterator(this);
}
@Override
public boolean hasNext() {
......@@ -57,7 +62,7 @@ public class Lexer implements Iterator<Token> {
}
void skipWhitespaces() {
private void skipWhitespaces() {
while(WHITESPACE.matcher(String.valueOf(it.peek())).matches()) {
it.junk();
}
......
package fr.insarennes.nperier.minichamo.parsing;
import fr.insarennes.nperier.minichamo.language.assignement.TypeDef;
import fr.insarennes.nperier.minichamo.language.typing.*;
import fr.insarennes.nperier.minichamo.language.typing.BuiltinType;
import fr.insarennes.nperier.minichamo.language.typing.FunctionType;
import fr.insarennes.nperier.minichamo.language.typing.GenericType;
import fr.insarennes.nperier.minichamo.language.typing.Nuple;
import fr.insarennes.nperier.minichamo.language.typing.Type;
import fr.insarennes.nperier.minichamo.lexing.Lexer;
import fr.insarennes.nperier.minichamo.lexing.tokens.TokenType;
import fr.insarennes.nperier.minichamo.utils.ParsingException;
......@@ -21,8 +24,7 @@ public class TypeParserTest {
@BeforeAll
public static void init() {
baseCtx = new Context();
Lexer l = new Lexer("type 'a 'b lol = ('a * 'b) list;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("type ('a, 'b) lol = ('a * 'b) list;;").wrap();
it.junkIf(TokenType.TYPEDEF);
TypedefParser.parse(it, baseCtx);
}
......@@ -34,8 +36,7 @@ public class TypeParserTest {
@Test
public void basicTypesTest() {
Lexer l = new Lexer("int;; string;; float;; bool;; unit;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("int;; string;; float;; bool;; unit;;").wrap();
for(String name : List.of("int", "string", "float", "bool", "unit")) {
Type expected = new BuiltinType(name);
Type t = TypeParser.parse(it, ctx);
......@@ -43,15 +44,13 @@ public class TypeParserTest {
Assertions.assertTrue(it.junkIf(TokenType.TERMINATION));
}
Assertions.assertFalse(it.hasNext());
l = new Lexer("int int;;");
TokenPeekIterator it1 = new TokenPeekIterator(l);
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it, ctx));
TokenPeekIterator it1 = new Lexer("int int;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it1, ctx));
}
@Test
public void genericTypeTest() {
Lexer l = new Lexer("'a;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("'a;;").wrap();
Type expected = new GenericType("a");
Type t = TypeParser.parse(it, ctx);
Assertions.assertEquals(expected, t);
......@@ -61,8 +60,7 @@ public class TypeParserTest {
@Test
public void nupleTypeTest() {
Lexer l = new Lexer("(int * (int * string) * bool);;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("(int * (int * string) * bool);;").wrap();
Type expected = new Nuple(List.of(
new BuiltinType("int"),
new Nuple(List.of(new BuiltinType("int"), new BuiltinType("string"))),
......@@ -73,15 +71,13 @@ public class TypeParserTest {
Assertions.assertTrue(it.junkIf(TokenType.TERMINATION));
Assertions.assertFalse(it.hasNext());
// If we put specifiers
l = new Lexer("int ('a * int);;");
TokenPeekIterator it1 = new TokenPeekIterator(l);
TokenPeekIterator it1 = new Lexer("int ('a * int);;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it1, ctx));
}
@Test
public void functionTypeTest() {
Lexer l = new Lexer("string -> bool -> (int -> int);;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("string -> bool -> (int -> int);;").wrap();
Type expected = new FunctionType(new Type[] {
new BuiltinType("string"),
new BuiltinType("bool"),
......@@ -98,8 +94,7 @@ public class TypeParserTest {
@Test
public void listTypeTest() {
Lexer l = new Lexer("int list;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("int list;;").wrap();
Type expected = new BuiltinType("list", new Type[]{
new BuiltinType("int")
});
......@@ -108,23 +103,19 @@ public class TypeParserTest {
Assertions.assertTrue(it.junkIf(TokenType.TERMINATION));
Assertions.assertFalse(it.hasNext());
// Too little specifiers
l = new Lexer("list;;");
TokenPeekIterator it1 = new TokenPeekIterator(l);
TokenPeekIterator it1 = new Lexer("list;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it1, ctx));
// Too little specifiers in parenthesis
l = new Lexer("(list) list;;");
TokenPeekIterator it2 = new TokenPeekIterator(l);
TokenPeekIterator it2 = new Lexer("(list) list;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it2, ctx));
// Too much specifiers
l = new Lexer("(int, int) list;;");
TokenPeekIterator it3 = new TokenPeekIterator(l);
TokenPeekIterator it3 = new Lexer("(int, int) list;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it3, ctx));
}
@Test
public void multipleSpecsTest() {
Lexer l = new Lexer("(int, string) lol;; ((int * int), string) lol;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("(int, string) lol;; ((int * int), string) lol;;").wrap();
Type userdef = ctx.getUserType("lol");
Type expected1 = userdef.specify(new Type[] {
new BuiltinType("int"),
......@@ -148,28 +139,23 @@ public class TypeParserTest {
@Test
public void invalidTypeTest() {
Lexer l = new Lexer("bonjour;;");
TokenPeekIterator it = new TokenPeekIterator(l);
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it, ctx));
l = new Lexer("];;");
TokenPeekIterator it1 = new TokenPeekIterator(l);
TokenPeekIterator it1 = new Lexer("bonjour;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it1, ctx));
TokenPeekIterator it2 = new Lexer("];;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it2, ctx));
}
@Test
public void forgotParenthesisTest() {
Lexer l = new Lexer("('a * int * string;;");
TokenPeekIterator it1 = new TokenPeekIterator(l);
TokenPeekIterator it1 = new Lexer("('a * int * string;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it1, ctx));
l = new Lexer("(int, int list;;");
TokenPeekIterator it2 = new TokenPeekIterator(l);
TokenPeekIterator it2 = new Lexer("(int, int list;;").wrap();
Assertions.assertThrows(ParsingException.class, () -> TypeParser.parse(it2, ctx));
}
@Test
public void complexSpecifiersTest() {
Lexer l = new Lexer("int -> int -> int list;; (((int * int) * int)) list;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("int -> int -> int list;; (((int * int) * int)) list;;").wrap();
Type expected1 = new FunctionType(new Type[]{
new BuiltinType("int"),
new BuiltinType("int"),
......@@ -197,8 +183,7 @@ public class TypeParserTest {
@Test
public void operatorPriorityTest() {
Lexer l = new Lexer("int -> int * int -> (int -> int);; (int -> int, int * int) lol;;");
TokenPeekIterator it = new TokenPeekIterator(l);
TokenPeekIterator it = new Lexer("int -> int * int -> (int -> int);; (int -> int, int * int) lol;;").wrap();
Type userdef = ctx.getUserType("lol");
Type expected1 = new FunctionType(new Type[] {
new BuiltinType("int"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment