static lex_tok_t* lexer_make_token(char* text) {
lex_tok_t* p_tok = NULL;
- if ((0 == strcmp(text,"end") || (text[0] == ';'))) {
+ if (0 == strcmp(text,"end")) {
p_tok = lex_tok_new(T_END, NULL);
} else if (lexer_oneof("()[]{};,'", text[0])) {
p_tok = lexer_punc(text);
break;
}
}
+ if (NULL == p_tok)
+ p_tok = lexer_var(text);
}
return p_tok;
}
static lex_tok_t* lexer_radix_int(char* text)
{
- return lexer_integer(&text[2], read_radix(text[1]));
+ lex_tok_t* ret = lexer_integer(&text[2], read_radix(text[1]));
+ if (NULL == ret)
+ ret = lexer_var(text);
+ return ret;
}
static lex_tok_t* lexer_number(char* text)
}
static int read_radix(char ch) {
+ int ret = -1;
switch(ch) {
- case 'b': return 2;
- case 'o': return 8;
- case 'd': return 10;
- case 'h': return 16;
- default: return 10;
+ case 'b': ret = 2; break;
+ case 'o': ret = 8; break;
+ case 'd': ret = 10; break;
+ case 'h': ret = 16; break;
}
+ return ret;
}
static char* scanner_read_string(scanner_t* p_scanner) {
size_t capacity = 8;
size_t index = 0;
- char* tok = (char*)malloc(sizeof(capacity));
+ char* tok = (char*)malloc(capacity);
/* Skip the first " */
tok[index++] = scanner_current(p_scanner);
/* Read the contents of the string */
while ('"' != scanner_current(p_scanner)) {
/* Resize the buffer if necessary */
- if ((index+2) >= capacity)
+ if ((index+2) >= capacity) {
capacity = capacity << 1;
+ tok = (char*)realloc(tok, capacity);
+ }
/* EOF results in an assertion (don't do) */
if (scanner_eof(p_scanner))
it "should recognize 'c'" do
expect(lexer('\c')).to eq ['T_CHAR:\c']
end
+
+ it "should recognize invalid named characters as identifiers" do
+ expect(lexer('\foobar')).to eq ['T_VAR:\foobar']
+ end
end
context "numbers" do
it "should recognize negitve integer with sign" do
expect(lexer('-123')).to eq ['T_INT:-123']
end
+
+ it "should recognize invalid ints as identifiers" do
+ expect(lexer('123a')).to eq ['T_VAR:123a']
+ end
end
context "radix integers" do
it "should recognize decimal integer" do
expect(lexer('0hf0f')).to eq ['T_INT:3855']
end
+
+ it "should recognize invalid radix ints as identifiers" do
+ expect(lexer('0b012')).to eq ['T_VAR:0b012']
+ end
end
context "floating point" do
expect(lexer('+123.0')).to eq ['T_FLOAT:123.000000']
end
- it "should recognize negitve float with sign" do
+ it "should recognize negative float with sign" do
expect(lexer('-123.0')).to eq ['T_FLOAT:-123.000000']
end
+
+ it "should recognize invalid floats as identifiers" do
+ expect(lexer('123..0')).to eq ['T_VAR:123..0']
+ end
end
end
it "should recognize a string that spans lines" do
expect(lexer("\"a\nb\"")).to eq ["T_STRING:\"a\nb\""]
end
+
+ it "should recognize larger strings" do
+ expect(lexer("\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"")).to eq [
+ "T_STRING:\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\""]
+ end
+
+ it "should raise an assertion exception when the file ends before a string terminates" do
+ expect{lexer("\"abc")}.to raise_error(/AssertionException/)
+ end
end
end