Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
issy committed Nov 17, 2024
1 parent 132228b commit 238f661
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/issy/Walker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.issy;

import com.issy.compiler.FileMatcher;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;

public class Walker {

public List<String> walk(String startPath, FileMatcher fileMatcher) {
try (Stream<Path> pathStream = Files.walk(Path.of(startPath))) {
return pathStream.map(Path::toString).filter(fileMatcher::matches).toList();
} catch (IOException e) {
throw new RuntimeException("Error reading file: " + e.getMessage());
} catch (SecurityException e) {
throw new RuntimeException("Not permitted to read file: " + e.getMessage());
}
}

}
34 changes: 26 additions & 8 deletions src/main/java/com/issy/compiler/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.issy.compiler.Token.*;

Expand All @@ -22,36 +23,37 @@ public Lexer(String input) {
public List<TokenContext> parse() {
final List<TokenContext> tokens = new ArrayList<>();
while (cursor.getValue() < input.length()) {
String currentChar = charAt(cursor.getValue());
final Position position = positionAt(cursor.getValue());
final String currentChar = charAt(cursor.getValue());
if (currentChar.equals("\"")) {
String munched = collectUntil(stringLiteralTerminationPredicate(cursor.getValue()));
tokens.add(new TokenContext(STRING, parseStringLiteral(munched)));
tokens.add(TokenContext.withPosition(STRING, parseStringLiteral(munched), position));
cursor.increment();
} else if (currentChar.equals("(")) {
tokens.add(new TokenContext(OPEN_PAREN, "("));
tokens.add(TokenContext.withPosition(OPEN_PAREN, "(", position));
cursor.increment();
} else if (currentChar.equals(")")) {
tokens.add(new TokenContext(CLOSE_PAREN, ")"));
tokens.add(TokenContext.withPosition(CLOSE_PAREN, ")", position));
cursor.increment();
} else if (currentChar.equals("!")) {
tokens.add(new TokenContext(NOT, "!"));
tokens.add(TokenContext.withPosition(NOT, "!", position));
cursor.increment();
} else if (currentChar.equals("&")) {
if (charAt(cursor.increment()).equals("&")) {
tokens.add(new TokenContext(OR, "&&"));
tokens.add(TokenContext.withPosition(OR, "&&", position));
} else {
throw new RuntimeException("Expected &&");
}
cursor.increment();
} else if (currentChar.equals("|")) {
if (charAt(cursor.increment()).equals("|")) {
tokens.add(new TokenContext(OR, "||"));
tokens.add(TokenContext.withPosition(OR, "||", position));
} else {
throw new RuntimeException("Expected ||");
}
cursor.increment();
} else if (Character.isLetter(input.charAt(cursor.getValue()))) {
tokens.add(new TokenContext(IDENTIFIER, collectUntil(identifierNameTerminationPredicate())));
tokens.add(TokenContext.withPosition(IDENTIFIER, collectUntil(identifierNameTerminationPredicate()), position));
} else if (currentChar.equals(" ") || currentChar.equals("\n") || currentChar.equals("\t") || currentChar.equals("\r")) {
cursor.increment();
continue;
Expand Down Expand Up @@ -93,6 +95,19 @@ private String charAt(int index) {
return String.valueOf(input.charAt(index));
}

private Position positionAt(int index) {
List<String> lines = input.lines().toList();
int count = 0;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
count += line.length();
if (count > index) {
return new Position(i + 1, index % count);
}
}
throw new IllegalArgumentException("Index exceeds string length");
}

private static class Cursor {

private int value;
Expand Down Expand Up @@ -126,4 +141,7 @@ public boolean atLimit() {
}
}

public static record Position(Integer line, Integer column) {
}

}
11 changes: 10 additions & 1 deletion src/main/java/com/issy/compiler/TokenContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.issy.compiler;

// TODO: Include line:col position data for better error messages
public record TokenContext(Token token, String value) {
public record TokenContext(Token token, String value, Integer line, Integer column) {

public static TokenContext withPosition(Token token, String value, Lexer.Position position) {
return new TokenContext(token, value, position.line(), position.column());
}

public String getFormattedPosition() {
return String.format("%d:%d", line + 1, column);
}

}
16 changes: 8 additions & 8 deletions src/test/java/com/issy/compiler/CompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class CompilerTest {
void canCompile() {
// Given
final List<TokenContext> tokenContexts = List.of(
new TokenContext(Token.IDENTIFIER, Identifier.HAS_FILE_EXTENSION.getName()),
new TokenContext(Token.OPEN_PAREN, "("),
new TokenContext(Token.STRING, "\".tsx\""),
new TokenContext(Token.CLOSE_PAREN, ")"),
new TokenContext(Token.AND, "&&"),
new TokenContext(Token.IDENTIFIER, Identifier.IS_IN_BASE_DIRECTORY.getName()),
new TokenContext(Token.OPEN_PAREN, "("),
new TokenContext(Token.CLOSE_PAREN, ")")
new TokenContext(Token.IDENTIFIER, Identifier.HAS_FILE_EXTENSION.getName(), 0, 0),
new TokenContext(Token.OPEN_PAREN, "(", 0, 0),
new TokenContext(Token.STRING, "\".tsx\"", 0, 0),
new TokenContext(Token.CLOSE_PAREN, ")", 0, 0),
new TokenContext(Token.AND, "&&", 0, 0),
new TokenContext(Token.IDENTIFIER, Identifier.IS_IN_BASE_DIRECTORY.getName(), 0, 0),
new TokenContext(Token.OPEN_PAREN, "(", 0, 0),
new TokenContext(Token.CLOSE_PAREN, ")", 0, 0)
);
final Compiler compiler = new Compiler(tokenContexts);

Expand Down

0 comments on commit 238f661

Please sign in to comment.