Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.ut.cs.akt.aktk;
- import static ee.ut.cs.akt.aktk.TokenType.*;
- import java.io.Reader;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Arrays;
- public class Lexer {
- private String[] parts;
- public Lexer(String input) {
- //input = input.toLowerCase();
- input = input.replaceAll( "//.*|(\"(?:\\\\[^\"]|\\\\\"|.)*?\")|(?s)/\\*.*?\\*/", "$1 " ); //eemaldab commentid, pretty smart huh?
- for (String symbol : new String[]{"+", "-", "*", "/", "(", ")", "{", "}"}) {
- input = input.replace(symbol, " " + symbol + " ");
- }
- this.parts = input.trim().split("\\s+");
- }
- public List<Token> readAllTokens() {
- System.out.println(Arrays.toString(parts));
- List<Token> result = new ArrayList<Token>();
- for (String part : parts) {
- if (part.equals("(")) {
- result.add(new Token(TokenType.LPAREN, null));
- }
- else if (part.equals(")")) {
- result.add(new Token(TokenType.RPAREN, null));
- }
- else if (part.equals("if")) {
- result.add(new Token(TokenType.IF, null));
- }
- else if (part.equals("while")) {
- result.add(new Token(TokenType.WHILE, null));
- }
- else if (part.equals("var")) {
- result.add(new Token(TokenType.VAR, null));
- }
- else if (part.equals("+")) {
- result.add(new Token(TokenType.PLUS, null));
- }
- else if (part.equals("-")) {
- result.add(new Token(TokenType.MINUS, null));
- }
- else if (part.equals("{")) {
- result.add(new Token(TokenType.LBRACE, null));
- }
- else if (part.equals("}")) {
- result.add(new Token(TokenType.RBRACE, null));
- }
- else if (part.equals("*")) {
- result.add(new Token(TokenType.TIMES, null));
- }
- else if (part.equals("/")) {
- result.add(new Token(TokenType.DIV, null));
- }
- else {
- try {
- Integer.parseInt(part);
- result.add(new Token(TokenType.INTEGER, Integer.parseInt(part)));
- } catch (NumberFormatException e) {
- try {
- Double.parseDouble(part);
- result.add(new Token(TokenType.DOUBLE, Double.parseDouble(part)));
- } catch (NumberFormatException e2) {
- //kontrollin kas string
- if ((part.startsWith("\"")) && (part.endsWith("\""))) {
- result.add(new Token(TokenType.STRING, part.replace("\"", "")));
- }
- else {
- // Nüüd peaks asi olema muutuja nimi.
- // Kontrolli, kas vastab muutuja nime reeglitele
- if ((!Character.isLetter(part.charAt(0))) && (!(part.startsWith("_")))) {
- throw new IllegalArgumentException("Muutujanimi peab algama tähe või alakriipsuga");
- }
- for (int i = 0; i < part.length(); i++) {
- if ((!Character.isLetterOrDigit(part.charAt(i))) && (part.charAt(i)!="_".charAt(0))) {
- throw new IllegalArgumentException("Muutujanimi ei tohi sisaldada midagi muud peale tähtede, numbrite ja alakriipsu");
- }
- }
- // kontroll läbitud, salvestame tokeni
- result.add(new Token(TokenType.VARIABLE, part));
- }
- }
- }
- }
- }
- result.add(new Token(TokenType.EOF, null));
- return result;
- }
- /**
- * Boonus
- *
- * Tagasta list, kus lekseemid on pakendatud koos infoga nende paiknemise kohta lähtekoodis
- */
- public List<PositionedToken> readAllPositionedTokens() {
- throw new UnsupportedOperationException();
- }
- /**
- * Boonus2
- *
- * Kui Lexer toetab Reader-iga konstruktorit, siis
- * readNextToken-i kasutamisel ei tohiks sisendit lugeda korraga rohkem,
- * kui järgmise lekseemi tuvastamiseks hädapärast tarvis.
- *
- * NB! Arvesta, et Reader-i markSupported() võib tagastada false.
- */
- public Lexer(Reader input) {
- throw new UnsupportedOperationException();
- }
- /**
- * See meetod peab olema implementeeritud vaid boonuse saamiseks
- */
- public Token readNextToken() {
- throw new UnsupportedOperationException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement