Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.InputMismatchException;
- import java.util.regex.Pattern;
- public class NumericBufferInput {
- //In order to use this function, pass a buffer, a String to print to the screen, and one of the lexer lambda expressions
- //within this class
- public static String applyLexerToBufferInput(BufferedReader ioStream, String printMe, LexString lexer) throws IOException {
- String abomination = null;
- boolean brrrrrramp;
- do {
- brrrrrramp = false;
- try {
- System.out.println(printMe);
- abomination = ioStream.readLine();
- lexer.lex(abomination);
- } catch (InputMismatchException e) {
- brrrrrramp = true;
- }
- } while (brrrrrramp);
- return abomination;
- }
- @FunctionalInterface
- public interface LexString {
- void lex(String s) throws InputMismatchException;
- }
- //determines if a string will parse as an Int/Long Literal that is non-octal
- public static final LexString isNaturalNumber = (String inputString) -> {
- var numberMatch = Pattern.compile("^(0|[1-9]\\d*)$").matcher(inputString);
- if (!numberMatch.find()) {
- throw new java.util.InputMismatchException();
- }
- };
- //determines if a string is Double/Float literal that is non-octal
- public static final LexString isFractional = (String amIFractional) -> {
- var fracMatch = Pattern.compile("^(0|\\d+?)\\.\\d+$").matcher(amIFractional);
- if (!fracMatch.find()) {
- throw new java.util.InputMismatchException();
- }
- };
- //determines if a string is either a Double/Float literal, or an Int/Long literal
- public static final LexString isWholeOrFractional = (String possibleNumber) -> {
- try {
- isFractional.lex(possibleNumber);
- } catch (java.util.InputMismatchException e) {
- isNaturalNumber.lex(possibleNumber);
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement