Byte/raw binary literal fixes

This commit is contained in:
Corey Richardson 2014-07-14 20:45:39 -07:00
parent 9fc5cf902f
commit f8fd32ef9d
3 changed files with 24 additions and 4 deletions

4
src/grammar/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
verify
*.class
*.java
*.tokens

View file

@ -44,6 +44,7 @@ SHR : '>>' ;
BINOP BINOP
: PLUS : PLUS
| SLASH
| MINUS | MINUS
| STAR | STAR
| PERCENT | PERCENT
@ -95,6 +96,10 @@ LIT_CHAR
: '\'' ( '\\' CHAR_ESCAPE | ~[\\'\n\t\r] ) '\'' : '\'' ( '\\' CHAR_ESCAPE | ~[\\'\n\t\r] ) '\''
; ;
LIT_BYTE
: 'b\'' ( '\\' ( [xX] HEXIT HEXIT | [nrt\\'"0] ) | ~[\\'\n\t\r] ) '\''
;
fragment INT_SUFFIX fragment INT_SUFFIX
: 'i' : 'i'
| 'i8' | 'i8'
@ -130,7 +135,7 @@ LIT_STR
; ;
LIT_BINARY : 'b' LIT_STR ; LIT_BINARY : 'b' LIT_STR ;
LIT_BINARY_RAW : 'b' LIT_STR_RAW ; LIT_BINARY_RAW : 'rb' LIT_STR_RAW ;
/* this is a bit messy */ /* this is a bit messy */
@ -159,7 +164,7 @@ OUTER_DOC_COMMENT : '//!' ~[\r\n]* -> type(DOC_COMMENT) ;
LINE_COMMENT : '//' ~[\r\n]* -> type(COMMENT) ; LINE_COMMENT : '//' ~[\r\n]* -> type(COMMENT) ;
DOC_BLOCK_COMMENT DOC_BLOCK_COMMENT
: ('/**' | '/*!') (DOC_BLOCK_COMMENT | .)*? '*/' -> type(DOC_COMMENT) : ('/**' ~[*] | '/*!') (DOC_BLOCK_COMMENT | .)*? '*/' -> type(DOC_COMMENT)
; ;
BLOCK_COMMENT : '/*' (BLOCK_COMMENT | .)*? '*/' -> type(COMMENT) ; BLOCK_COMMENT : '/*' (BLOCK_COMMENT | .)*? '*/' -> type(COMMENT) ;

View file

@ -71,6 +71,7 @@ fn parse_token_list(file: &str) -> HashMap<String, Token> {
"IDENT" => id(), "IDENT" => id(),
"PLUS" => BINOP(PLUS), "PLUS" => BINOP(PLUS),
"LIT_CHAR" => LIT_CHAR(Name(0)), "LIT_CHAR" => LIT_CHAR(Name(0)),
"LIT_BYTE" => LIT_BYTE(Name(0)),
"EQ" => EQ, "EQ" => EQ,
"RBRACKET" => RBRACKET, "RBRACKET" => RBRACKET,
"COMMENT" => COMMENT, "COMMENT" => COMMENT,
@ -124,7 +125,7 @@ fn str_to_binop(s: &str) -> BinOp {
} }
} }
/// Assuming a raw string/binary literal, strip out the leading/trailing /// Assuming a string/binary literal, strip out the leading/trailing
/// hashes and surrounding quotes/raw/binary prefix. /// hashes and surrounding quotes/raw/binary prefix.
fn fix(mut lit: &str) -> ast::Name { fn fix(mut lit: &str) -> ast::Name {
if lit.char_at(0) == 'r' { if lit.char_at(0) == 'r' {
@ -143,6 +144,15 @@ fn fix(mut lit: &str) -> ast::Name {
parse::token::intern(lit.slice(leading_hashes + 1, lit.len() - leading_hashes - 1)) parse::token::intern(lit.slice(leading_hashes + 1, lit.len() - leading_hashes - 1))
} }
/// Assuming a char/byte literal, strip the 'b' prefix and the single quotes.
fn fixchar(mut lit: &str) -> ast::Name {
if lit.char_at(0) == 'b' {
lit = lit.slice_from(1);
}
parse::token::intern(lit.slice(1, lit.len() - 1))
}
fn count(lit: &str) -> uint { fn count(lit: &str) -> uint {
lit.chars().take_while(|c| *c == '#').count() lit.chars().take_while(|c| *c == '#').count()
} }
@ -167,7 +177,8 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, Token>) -> TokenAndSpan {
BINOPEQ(..) => BINOPEQ(str_to_binop(content.slice_to(content.len() - 1))), BINOPEQ(..) => BINOPEQ(str_to_binop(content.slice_to(content.len() - 1))),
LIT_STR(..) => LIT_STR(fix(content)), LIT_STR(..) => LIT_STR(fix(content)),
LIT_STR_RAW(..) => LIT_STR_RAW(fix(content), count(content)), LIT_STR_RAW(..) => LIT_STR_RAW(fix(content), count(content)),
LIT_CHAR(..) => LIT_CHAR(nm), LIT_CHAR(..) => LIT_CHAR(fixchar(content)),
LIT_BYTE(..) => LIT_BYTE(fixchar(content)),
DOC_COMMENT(..) => DOC_COMMENT(nm), DOC_COMMENT(..) => DOC_COMMENT(nm),
LIT_INTEGER(..) => LIT_INTEGER(nm), LIT_INTEGER(..) => LIT_INTEGER(nm),
LIT_FLOAT(..) => LIT_FLOAT(nm), LIT_FLOAT(..) => LIT_FLOAT(nm),