1 23 24 package net.percederberg.grammatica.test; 25 26 import java.io.Reader ; 27 28 import net.percederberg.grammatica.parser.ParserCreationException; 29 import net.percederberg.grammatica.parser.TokenPattern; 30 import net.percederberg.grammatica.parser.Tokenizer; 31 32 38 class ArithmeticTokenizer extends Tokenizer { 39 40 48 public ArithmeticTokenizer(Reader input) 49 throws ParserCreationException { 50 51 super(input, false); 52 createPatterns(); 53 } 54 55 61 private void createPatterns() throws ParserCreationException { 62 TokenPattern pattern; 63 64 pattern = new TokenPattern(ArithmeticConstants.ADD, 65 "ADD", 66 TokenPattern.STRING_TYPE, 67 "+"); 68 addPattern(pattern); 69 70 pattern = new TokenPattern(ArithmeticConstants.SUB, 71 "SUB", 72 TokenPattern.STRING_TYPE, 73 "-"); 74 addPattern(pattern); 75 76 pattern = new TokenPattern(ArithmeticConstants.MUL, 77 "MUL", 78 TokenPattern.STRING_TYPE, 79 "*"); 80 addPattern(pattern); 81 82 pattern = new TokenPattern(ArithmeticConstants.DIV, 83 "DIV", 84 TokenPattern.STRING_TYPE, 85 "/"); 86 addPattern(pattern); 87 88 pattern = new TokenPattern(ArithmeticConstants.LEFT_PAREN, 89 "LEFT_PAREN", 90 TokenPattern.STRING_TYPE, 91 "("); 92 addPattern(pattern); 93 94 pattern = new TokenPattern(ArithmeticConstants.RIGHT_PAREN, 95 "RIGHT_PAREN", 96 TokenPattern.STRING_TYPE, 97 ")"); 98 addPattern(pattern); 99 100 pattern = new TokenPattern(ArithmeticConstants.NUMBER, 101 "NUMBER", 102 TokenPattern.REGEXP_TYPE, 103 "[0-9]+"); 104 addPattern(pattern); 105 106 pattern = new TokenPattern(ArithmeticConstants.IDENTIFIER, 107 "IDENTIFIER", 108 TokenPattern.REGEXP_TYPE, 109 "[a-z]"); 110 addPattern(pattern); 111 112 pattern = new TokenPattern(ArithmeticConstants.WHITESPACE, 113 "WHITESPACE", 114 TokenPattern.REGEXP_TYPE, 115 "[ \\t\\n\\r]+"); 116 pattern.setIgnore(); 117 addPattern(pattern); 118 } 119 } 120 | Popular Tags |