Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zavada.core;
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.zavada.symbol.Token;
- public class Lexer {
- private static String[] regex = {
- ":(.+?):", //VARIABLE
- "(\\d+)?(\\.?\\d+)", //DIGIT LITERAL
- "\".*\"", //STRING LITERAL
- "[%*+-\\/]", //OPERATOR
- "=", //ASSIGNMENT
- "P", //PRINT
- };
- public static ArrayList<Token> lex(String in){
- ArrayList<Token> tokens = new ArrayList<Token>();
- while(in.trim().length() > 0) {
- int index = in.length();
- String foundRegex = null;
- for(String a : regex) {
- int testIndex = indexOf(Pattern.compile(a),in);
- if( testIndex < index && testIndex > -1) {
- index = testIndex;
- foundRegex = a;
- }
- }
- System.out.println(in);
- Pattern pattern = Pattern.compile(foundRegex);
- Matcher matcher = pattern.matcher(in);
- if(matcher.find()) {
- tokens.add(new Token(foundRegex, matcher.group(0)));
- }
- in = in.replaceFirst(foundRegex, "");
- }
- return tokens;
- }
- /** @return index of pattern in s or -1, if not found */
- public static int indexOf(Pattern pattern, String s) {
- Matcher matcher = pattern.matcher(s);
- return matcher.find() ? matcher.start() : -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement