Skip to content
Snippets Groups Projects
Commit b7b368c6 authored by Dominic Daniel Krämer's avatar Dominic Daniel Krämer
Browse files

added comments on the methods & classes

parent f914c527
No related branches found
No related tags found
1 merge request!3added comments on the methods & classes
...@@ -3,9 +3,13 @@ import java.util.List; ...@@ -3,9 +3,13 @@ import java.util.List;
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
//List with a fitting String to be parsed into multiple tokens
List<String> test = Arrays.asList("1 + xy^2 - (31 * x)y"); List<String> test = Arrays.asList("1 + xy^2 - (31 * x)y");
//Iterates through every String
for (String value: test) { for (String value: test) {
//creates a list of tokens out of the String
List<Lexer.Token> tokens = Lexer.lex(value); List<Lexer.Token> tokens = Lexer.lex(value);
//prints each token out (with .toString())
for(Lexer.Token singleToken: tokens) { for(Lexer.Token singleToken: tokens) {
System.out.println(singleToken.toString()); System.out.println(singleToken.toString());
} }
......
...@@ -2,10 +2,14 @@ import java.util.LinkedList; ...@@ -2,10 +2,14 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
public class Lexer { public class Lexer {
//enum TokenType that can either be a number, variable or a special character (like * or / etc.)
public static enum TokenType { public static enum TokenType {
NUMBER, VARIABLE, SPECIAL NUMBER, VARIABLE, SPECIAL
} }
//tokens with a TokenType that represents their type and a String with the actual data
//has for both values a getter method and overrides toString with it's fitting values
public static class Token { public static class Token {
protected TokenType type; protected TokenType type;
protected String data; protected String data;
...@@ -28,11 +32,16 @@ public class Lexer { ...@@ -28,11 +32,16 @@ public class Lexer {
} }
} }
//creates a list of tokens from a given String
public static List<Token> lex(String input) { public static List<Token> lex(String input) {
//the list that will be returned at the end
List<Token> result = new LinkedList<Token>(); List<Token> result = new LinkedList<Token>();
//for-loop that iterates through each character of the String
for(int index=0; index < input.length();) { for(int index=0; index < input.length();) {
char current = input.charAt(index); char current = input.charAt(index);
//checks if a character is a digit, then creates a token with one or multiple numbers
//with TokenType=NUMBER
if(Character.isDigit(current)) { if(Character.isDigit(current)) {
int endIndex = index+1; int endIndex = index+1;
if(endIndex<input.length()) { if(endIndex<input.length()) {
...@@ -44,9 +53,8 @@ public class Lexer { ...@@ -44,9 +53,8 @@ public class Lexer {
result.add(token); result.add(token);
index += endIndex - index; index += endIndex - index;
} }
else if(Character.isWhitespace(current)) { //checks if a character is a letter, then creates for each following character a separate token
index++; //with TokenType=VARIABLE
}
else if(Character.isLetter(current)){ else if(Character.isLetter(current)){
int iterator = index; int iterator = index;
while(Character.isLetter(input.charAt(iterator)) && iterator<input.length()) { while(Character.isLetter(input.charAt(iterator)) && iterator<input.length()) {
...@@ -59,6 +67,12 @@ public class Lexer { ...@@ -59,6 +67,12 @@ public class Lexer {
} }
index += iterator - index; index += iterator - index;
} }
//whitespaces are ignored
else if(Character.isWhitespace(current)) {
index++;
}
//every char of the string that isn't a digit, letter or whitespace will be
//used for creating a token with TokenType=SPECIAL
else { else {
Token token = new Token(TokenType.SPECIAL, String.valueOf(current)); Token token = new Token(TokenType.SPECIAL, String.valueOf(current));
result.add(token); result.add(token);
......
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