Make some u32s be u16s

This commit is contained in:
Richard Feldman 2019-09-03 20:16:56 -04:00
parent e3592a6dd7
commit 6a77f00ff9
5 changed files with 42 additions and 40 deletions

View file

@ -62,7 +62,7 @@ where
})
}
fn indentation<I>() -> impl Parser<Input = I, Output = u32>
fn indentation<I>() -> impl Parser<Input = I, Output = u16>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -136,7 +136,7 @@ where
.with(value(()))
}
fn indented_whitespaces<I>(min_indent: u32) -> impl Parser<Input = I, Output = ()>
fn indented_whitespaces<I>(min_indent: u16) -> impl Parser<Input = I, Output = ()>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -144,7 +144,7 @@ where
skip_many(skipped_indented_whitespace_char(min_indent))
}
fn indented_whitespaces1<I>(min_indent: u32) -> impl Parser<Input = I, Output = ()>
fn indented_whitespaces1<I>(min_indent: u16) -> impl Parser<Input = I, Output = ()>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -152,7 +152,7 @@ where
skip_many1(skipped_indented_whitespace_char(min_indent))
}
fn skipped_indented_whitespace_char<I>(min_indent: u32) -> impl Parser<Input = I, Output = ()>
fn skipped_indented_whitespace_char<I>(min_indent: u16) -> impl Parser<Input = I, Output = ()>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -186,7 +186,7 @@ where
/// This is separate from expr_body for the sake of function application,
/// so it can stop parsing when it reaches an operator (since they have
/// higher precedence.)
fn function_arg_expr<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
fn function_arg_expr<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -196,10 +196,10 @@ where
parser! {
#[inline(always)]
fn function_arg_expr_[I](min_indent_ref: u32)(I) -> Expr
fn function_arg_expr_[I](min_indent_ref: u16)(I) -> Expr
where [ I: Stream<Item = char, Position = IndentablePosition> ]
{
// TODO figure out why min_indent_ref has the type &mut u32
// TODO figure out why min_indent_ref has the type &mut u16
let min_indent = *min_indent_ref;
// Rules for expressions that can go in function arguments:
@ -225,7 +225,7 @@ parser! {
}
}
fn expr_body<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
fn expr_body<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -236,10 +236,10 @@ where
// This macro allows recursive parsers
parser! {
#[inline(always)]
fn expr_body_[I](min_indent_ref: u32)(I) -> Expr
fn expr_body_[I](min_indent_ref: u16)(I) -> Expr
where [ I: Stream<Item = char, Position = IndentablePosition> ]
{
// TODO figure out why min_indent_ref has the type &mut u32
// TODO figure out why min_indent_ref has the type &mut u16
let min_indent = *min_indent_ref;
located(choice((
@ -272,7 +272,7 @@ parser! {
}
}
pub fn if_expr<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn if_expr<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -296,7 +296,7 @@ where
})
}
pub fn case_expr<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn case_expr<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -324,7 +324,7 @@ where
})
}
pub fn list<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn list<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -348,7 +348,7 @@ where
})
}
pub fn apply_with_parens<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn apply_with_parens<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -376,7 +376,7 @@ where
}
#[inline(always)]
fn function_arg<I>(min_indent: u32) -> impl Parser<Input = I, Output = Located<Expr>>
fn function_arg<I>(min_indent: u16) -> impl Parser<Input = I, Output = Located<Expr>>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -405,7 +405,7 @@ where
)))
}
pub fn apply_args<I>(min_indent: u32) -> impl Parser<Input = I, Output = Vec<Located<Expr>>>
pub fn apply_args<I>(min_indent: u16) -> impl Parser<Input = I, Output = Vec<Located<Expr>>>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -493,7 +493,7 @@ where
}
pub fn nested_assignment<I>(
min_indent: u32,
min_indent: u16,
) -> impl Parser<Input = I, Output = (Located<Pattern>, Located<Expr>)>
where
I: Stream<Item = char, Position = IndentablePosition>,
@ -530,7 +530,7 @@ where
)
}
pub fn assignment<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn assignment<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -570,7 +570,7 @@ where
})
}
pub fn func_or_var<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn func_or_var<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -594,7 +594,7 @@ where
}
/// e.g. \x y => expr
pub fn closure<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn closure<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -616,7 +616,7 @@ where
parser! {
#[inline(always)]
fn pattern[I](min_indent_ref: u32)(I) -> Pattern
fn pattern[I](min_indent_ref: u16)(I) -> Pattern
where [ I: Stream<Item = char, Position = IndentablePosition> ]
{
let min_indent = *min_indent_ref;
@ -631,7 +631,7 @@ parser! {
}
}
pub fn apply_variant<I>(min_indent: u32) -> impl Parser<Input = I, Output = Expr>
pub fn apply_variant<I>(min_indent: u16) -> impl Parser<Input = I, Output = Expr>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
@ -643,7 +643,7 @@ where
})
}
pub fn match_variant<I>(min_indent: u32) -> impl Parser<Input = I, Output = Pattern>
pub fn match_variant<I>(min_indent: u16) -> impl Parser<Input = I, Output = Pattern>
where
I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>,

View file

@ -18,10 +18,10 @@ pub struct IndentablePosition {
/// Current line of the input
pub line: u32,
/// Current column of the input
pub column: u32,
pub column: u16,
/// Current indentation level, in columns (so no indent is col 1 - this saves an arithmetic operation.)
pub indent_col: u32,
pub indent_col: u16,
// true at the beginning of each line, then false after encountering the first nonspace char.
pub is_indenting: bool,

View file

@ -7,9 +7,6 @@ use parse::ast::Expr;
use parse::parser::Parser;
use parse::string_literal::string_literal;
pub fn expr<'a, 'p>() -> impl Parser<'a, 'p, Expr<'a>>
where
'p: 'a,
{
pub fn expr<'a>() -> impl Parser<'a, Expr<'a>> {
string_literal()
}

View file

@ -99,7 +99,7 @@ fn escaped_char_problem<'a, 'p>(
buf_len: usize,
) {
let start_line = state.line;
let start_col = state.column + buf_len as u32;
let start_col = state.column + buf_len as u16;
let end_line = start_line;
// escapes should all be 2 chars long
let end_col = state.column + 1;
@ -126,11 +126,11 @@ fn escaped_unicode_problem<'a, 'p>(
) {
let start_line = state.line;
// +1 due to the `"` which precedes buf.
let start_col = state.column + buf_len as u32 + 1;
let start_col = state.column + buf_len as u16 + 1;
let end_line = start_line;
// +3 due to the `\u{` and another + 1 due to the `}`
// -1 to prevent overshooting because end col is inclusive.
let end_col = start_col + 3 + hex_str_len as u32 + 1 - 1;
let end_col = start_col + 3 + hex_str_len as u16 + 1 - 1;
let region = Region {
start_line,
@ -209,7 +209,7 @@ where
if chars.next() != Some('{') {
let start_line = state.line;
// +1 due to the `"` which precedes buf
let start_col = state.column + 1 + buf.len() as u32;
let start_col = state.column + 1 + buf.len() as u16;
let end_line = start_line;
// All we parsed was `\u`, so end on the column after `\`'s column.
@ -251,13 +251,13 @@ where
let start_line = state.line;
// +1 due to the `"` which precedes buf
// +3 due to the `\u{` which precedes the hex digits
let start_col = state.column + 1 + buf.len() as u32 + 3;
let start_col = state.column + 1 + buf.len() as u16 + 3;
let end_line = start_line;
// We want to underline only the number. That's the error!
// -1 because we want to end on the last digit, not
// overshoot it.
let end_col = start_col + hex_str.len() as u32 - 1;
let end_col = start_col + hex_str.len() as u16 - 1;
let region = Region {
start_line,
@ -355,11 +355,11 @@ where
// parsing logic can consume the quote and do its job as normal.
let start_line = state.line;
// +1 due to the `"` which precedes buf.
let start_col = state.column + buf.len() as u32 + 1;
let start_col = state.column + buf.len() as u16 + 1;
let end_line = start_line;
// +3 due to the `\u{`
// -1 to prevent overshooting because end col is inclusive.
let end_col = start_col + 3 + hex_str.len() as u32 - 1;
let end_col = start_col + 3 + hex_str.len() as u16 - 1;
let region = Region {
start_line,

View file

@ -5,11 +5,16 @@ pub type Loc<T> = Located<T>;
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct Region {
pub start_col: u16,
pub end_col: u16,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
}
#[test]
fn region_size() {
// Region is used all over the place. Avoid increasing its size!
assert_eq!(std::mem::size_of::<Region>(), 8);
}
impl fmt::Debug for Region {