Skip to content

Commit 1cbc630

Browse files
committed
Añadido el lexeo de strings, y pruebas
1 parent 2094f0c commit 1cbc630

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/correctorcodigodoc/Lexer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ private Token nextTokenInternal() {
9797
return ret;
9898
}
9999

100+
// Parsea una string que comienza por ' o "
101+
if (ultimo_caracter == '\'' || ultimo_caracter == '"') {
102+
// Come caracteres hasta que encuentres el cerrador
103+
char iniciador = ultimo_caracter;
104+
String string = "";
105+
106+
while (hasNextChar() && (ultimo_caracter = getChar()) != iniciador)
107+
string += String.valueOf(ultimo_caracter);
108+
109+
// Comete el cerrador
110+
eatNextChar();
111+
112+
return new Token(Token.Ids.STRING, string, lex_index);
113+
}
114+
100115
// Parsea una palabra vorazmente
101116
if (Character.isLetter(ultimo_caracter) || ultimo_caracter == '_') {
102117
String identificador = "";

test/tests/LexerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,17 @@ public void TestMenos() {
155155
assertEquals(Token.Ids.NUMERO, token.id);
156156
assertEquals("65", token.texto);
157157
}
158+
159+
@Test
160+
public void TestString() {
161+
Lexer lexer = new Lexer("'He\"llo \"the\"re!' \" OH'AI \"");
162+
163+
Token token = lexer.nextToken();
164+
assertEquals(Token.Ids.STRING, token.id);
165+
assertEquals("He\"llo \"the\"re!", token.texto);
166+
167+
token = lexer.nextToken();
168+
assertEquals(Token.Ids.STRING, token.id);
169+
assertEquals(" OH'AI ", token.texto);
170+
}
158171
}

0 commit comments

Comments
 (0)