forked from google/codeu_coding_assessment_b_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTokenReader.java
More file actions
133 lines (119 loc) · 3.58 KB
/
MyTokenReader.java
File metadata and controls
133 lines (119 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.codeu.mathlang.impl;
import java.io.IOException;
import com.google.codeu.mathlang.core.tokens.*;
import com.google.codeu.mathlang.parsing.TokenReader;
import javax.naming.Name;
// MY TOKEN READER
//
// This is YOUR implementation of the token reader interface. To know how
// it should work, read src/com/google/codeu/mathlang/parsing/TokenReader.java.
// You should not need to change any other files to get your token reader to
// work with the test of the system.
public final class MyTokenReader implements TokenReader {
String source;
public MyTokenReader(String source) {
// Your token reader will only be given a string for input. The string will
// contain the whole source (0 or more lines).
this.source=source;
}
@Override
public Token next() throws IOException {
// Most of your work will take place here. For every call to |next| you should
// return a token until you reach the end. When there are no more tokens, you
// should return |null| to signal the end of input.
// If for any reason you detect an error in the input, you may throw an IOException
// which will stop all execution.
source = source.trim();
return getToken();
}
public boolean isSymbol(char inpChar)
{
return (inpChar==';' || inpChar=='+' || inpChar=='-' || inpChar=='*' || inpChar=='/' || inpChar=='=');
}
public Token getToken() throws IOException
{
if(source.length()==0)
{
return null;
}
char firstChar = source.charAt(0);
if(Character.isDigit(firstChar))
{
return getNumber();
}
else if(Character.isLetter(firstChar))
{
return getName();
}
else if(firstChar=='\"')
{
return getStringTok();
}
else if(isSymbol(firstChar))
{
return getSymbol();
}
throw new IOException();
}
public NameToken getName()
{
int stop = 0;
while(Character.isLetter(source.charAt(stop)))
{
stop++;
}
String name = source.substring(0,stop);
name.replaceAll(" ", "");
source = source.substring(stop);
return new NameToken(name);
}
public NumberToken getNumber()
{
int index = 0;
while(index<source.length()&&Character.isDigit(source.charAt(index)))
{
index++;
}
if(index<source.length()&&source.charAt(index)=='.')
{
index++;
}
while(index<source.length()&&Character.isDigit(source.charAt(index)))
{
index++;
}
String number = source.substring(0,index);
source = source.substring(index);
return new NumberToken(Double.parseDouble(number));
}
public StringToken getStringTok() throws IOException
{
int stop = source.indexOf('\"', source.indexOf('\"') + 1);
if(stop==-1)
{
throw new IOException();
}
String string = source.substring(1,stop);
source = source.substring(stop+1);
return new StringToken(string);
}
public SymbolToken getSymbol()
{
char symbol = source.charAt(0);
source = source.substring(1);
return new SymbolToken(symbol);
}
}