import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Parser
{
private boolean success;
private Parser.Scanner scanner;
private Parser()
{
}
public Parser(String paramString)
{
this.success = false;
try
{
this.scanner = new Parser.Scanner(paramString);
}
catch (FileNotFoundException localFileNotFoundException)
{
System.out.println("Unable to find file " + paramString);
return;
}
while ((localLexeme = this.scanner.getNextLexeme()) != null)
{
Lexeme localLexeme;
System.out.print("The scanner found a lexeme with source code \"" + localLexeme.getSource() + "\" at line ");
System.out.println(localLexeme.getLineNumber() + " and assigned token " + localLexeme.getToken().name());
}
this.success = true;
}
public boolean isSuccessful()
{
return this.success;
}
private static class Scanner
{
private Scanner javaScanner;
private Pattern pattern;
private Matcher matcher;
private String sourceLine;
private int lineNumber;
private Scanner()
{
}
public Scanner(String paramString)
throws FileNotFoundException
{
this.sourceLine = "";
this.lineNumber = 0;
this.javaScanner = new Scanner(new File(paramString));
}
public Lexeme getNextLexeme()
{
if (this.sourceLine.length() == 0)
{
if (!this.javaScanner.hasNextLine())
{
return null;
}
this.sourceLine = this.javaScanner.nextLine();
this.lineNumber += 1;
}
this.pattern = Pattern.compile("\\s+");
this.matcher = this.pattern.matcher(this.sourceLine);
if (this.matcher.lookingAt())
{
this.sourceLine = this.sourceLine.substring(this.matcher.group().length());
}
for (Lexeme.Token localToken : Lexeme.Token.values())
{
this.pattern = Pattern.compile(localToken.getRegex());
this.matcher = this.pattern.matcher(this.sourceLine);
if (!this.matcher.lookingAt())
continue;
this.sourceLine = this.sourceLine.substring(this.matcher.group().length());
return new Lexeme(this.matcher.group(), this.lineNumber, localToken);
}
return null;
}
}
}