mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-08 04:56:58 +00:00
Begin work on #fmt parsing
This commit is contained in:
parent
9528c34774
commit
1c60399257
@ -13,8 +13,10 @@
|
||||
* combinations at the moment.
|
||||
*/
|
||||
|
||||
use std;
|
||||
import front.parser;
|
||||
|
||||
import std._str;
|
||||
import std._vec;
|
||||
import std.option;
|
||||
|
||||
tag signedness {
|
||||
@ -64,14 +66,108 @@ tag conv {
|
||||
// A fragment of the output sequence
|
||||
tag piece {
|
||||
piece_string(str);
|
||||
piece_conv(str);
|
||||
piece_conv(conv);
|
||||
}
|
||||
|
||||
fn bad_fmt_call() {
|
||||
log "malformed #fmt call";
|
||||
fail;
|
||||
}
|
||||
|
||||
fn expand_syntax_ext(vec[@ast.expr] args,
|
||||
option.t[@ast.expr] body) -> @ast.expr {
|
||||
|
||||
if (_vec.len[@ast.expr](args) == 0u) {
|
||||
bad_fmt_call();
|
||||
}
|
||||
|
||||
auto fmt = expr_to_str(args.(0));
|
||||
log fmt;
|
||||
auto pieces = parse_fmt_string(fmt);
|
||||
ret pieces_to_expr(pieces, args);
|
||||
}
|
||||
|
||||
fn expr_to_str(@ast.expr expr) -> str {
|
||||
alt (expr.node) {
|
||||
case (ast.expr_lit(?l, _)) {
|
||||
alt (l.node) {
|
||||
case (ast.lit_str(?s)) {
|
||||
ret s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bad_fmt_call();
|
||||
fail;
|
||||
}
|
||||
|
||||
fn parse_fmt_string(str s) -> vec[piece] {
|
||||
let vec[piece] pieces = vec();
|
||||
// FIXME: Should be counting codepoints instead of bytes
|
||||
auto lim = _str.byte_len(s);
|
||||
auto buf = "";
|
||||
|
||||
// TODO: This is super ugly
|
||||
fn flush_buf(str buf, vec[piece] pieces) -> str {
|
||||
log "flushing";
|
||||
if (_str.byte_len(buf) > 0u) {
|
||||
auto piece = piece_string(buf);
|
||||
pieces += piece;
|
||||
}
|
||||
log "buf:";
|
||||
log buf;
|
||||
log "pieces:";
|
||||
for (piece p in pieces) {
|
||||
alt (p) {
|
||||
case (piece_string(?s)) {
|
||||
log s;
|
||||
}
|
||||
case (piece_conv(_)) {
|
||||
log "conv";
|
||||
}
|
||||
}
|
||||
}
|
||||
ret "";
|
||||
}
|
||||
|
||||
auto i = 0u;
|
||||
while (i < lim) {
|
||||
log "step:";
|
||||
log i;
|
||||
auto curr = _str.substr(s, i, 1u);
|
||||
if (_str.eq(curr, "%")) {
|
||||
i += 1u;
|
||||
if (i >= lim) {
|
||||
log "unterminated conversion at end of string";
|
||||
fail;
|
||||
}
|
||||
auto curr2 = _str.substr(s, i, 1u);
|
||||
if (_str.eq(curr2, "%")) {
|
||||
i += 1u;
|
||||
} else {
|
||||
buf = flush_buf(buf, pieces);
|
||||
}
|
||||
} else {
|
||||
buf += curr;
|
||||
log "buf:";
|
||||
log buf;
|
||||
i += 1u;
|
||||
}
|
||||
}
|
||||
|
||||
ret pieces;
|
||||
}
|
||||
|
||||
fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
|
||||
auto lo = args.(0).span;
|
||||
auto hi = args.(0).span;
|
||||
auto strlit = ast.lit_str("TODO");
|
||||
auto spstrlit = @parser.spanned[ast.lit_](lo, hi, strlit);
|
||||
auto expr = ast.expr_lit(spstrlit, ast.ann_none);
|
||||
auto spexpr = @parser.spanned[ast.expr_](lo, hi, expr);
|
||||
ret spexpr;
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
@ -756,7 +756,6 @@ impure fn expand_syntax_ext(parser p, @ast.expr ext) -> @ast.expr {
|
||||
auto extname = path.node.idents.(0);
|
||||
if (_str.eq(extname, "fmt")) {
|
||||
auto expanded = extfmt.expand_syntax_ext(args, body);
|
||||
check (ast.is_ext_expr(expanded));
|
||||
auto newexpr = ast.expr_ext(path, args, body,
|
||||
some[@ast.expr](expanded), ann);
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
use std;
|
||||
import std._str;
|
||||
//use std;
|
||||
//import std._str;
|
||||
|
||||
fn test(str actual, str expected) {
|
||||
log actual;
|
||||
log expected;
|
||||
check (_str.eq(actual, expected));
|
||||
//check (_str.eq(actual, expected));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
Loading…
Reference in New Issue
Block a user