sub parse_error {
my ($self, $token, $msg) = @_;
my $line = $token ? $token->{line} : 'EOF';
- if ($self->{annotated}) {
+ if ($self->{annotated} || $ENV{TOML_TINY_DEBUG}) {
my $root = Dumper($self->{root});
my $tok = Dumper($token);
- my $src = substr $self->{tokenizer}{source}, $self->{tokenizer}{position} - 20, 40;
+ my $src = substr $self->{tokenizer}{source}, $self->{tokenizer}{position}, 30;
confess qq{
toml parse error at line $line:
source => $param{source},
last_position => length $param{source},
position => 0,
- line => 0,
+ line => 1,
tokens => [],
}, $class;
return $self;
}
+sub prev_token_type {
+ my $self = shift;
+
+ if (@{$self->{tokens}}) {
+ return $self->{tokens}[-1]{type} // 'EOL';
+ }
+
+ return 'EOL';
+}
+
sub next_token {
my $self = shift;
state $key = qr/(?&Key) $TOML/x;
while ($self->{position} < $self->{last_position} && !$token) {
+ my $prev = $self->prev_token_type;
+ my $newline = !!($prev eq 'EOL' || $prev eq 'table' || $prev eq 'array_table');
+
for ($self->{source}) {
- when (/\G [\x20 \x09]+/xgc) {
- ;
- }
+ /\G[\x20\x09]+/gc; # ignore whitespace
+ /\G\x23.*/gc && next; # ignore comments
- when (/\G \x23 .*/xgc) {
- ;
- }
+ last when /\G $/xgc;
when (/\G \x0D? \x0A/xgc) {
++$self->{line};
$token = $self->_make_token('EOL');
}
- when (/\G \[ [\x20 \x09]* ($key) [\x20 \x09]* \] [\x20 \x09]* (?= (:? \x23 .* )? (?: \x0D? \x0A) | $ )/xgc) {
- $token = $self->_make_token('table', $self->tokenize_key($1));
- }
+ if ($newline) {
+ when (/\G \[ [\x20 \x09]* ($key) [\x20 \x09]* \] [\x20 \x09]* (?= (:? \x23 .* )? (?: \x0D? \x0A) | $ )/xgc) {
+ $token = $self->_make_token('table', $self->tokenize_key($1));
+ }
- when (/\G \[\[ [\x20 \x09]* ($key) [\x20 \x09]* \]\] [\x20 \x09]* (?= (:? \x23 .* )? (?: \x0D? \x0A) | $ )/xgc) {
- $token = $self->_make_token('array_table', $self->tokenize_key($1));
+ when (/\G \[\[ [\x20 \x09]* ($key) [\x20 \x09]* \]\] [\x20 \x09]* (?= (:? \x23 .* )? (?: \x0D? \x0A) | $ )/xgc) {
+ $token = $self->_make_token('array_table', $self->tokenize_key($1));
+ }
}
when (/\G \[ /xgc) {
}
when (/\G ($key) [\x20 \x09]* (?= =)/xgc) {
- $token = $self->_make_token('key', $1);
+ $token = $self->_make_token('key', $1);
}
when (/\G ((?&Boolean)) $TOML/xgc) {