Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub enum Token<'a> {
#[token("function", accept_expression)]
#[token("func", |lex| parse_non_posix_keyword(lex, Token::Function))]
Function,
#[token("length", accept_expression)]
#[token("length", accept_operator)]
Length,
#[token("substr", accept_expression)]
Substr,
Expand Down
13 changes: 10 additions & 3 deletions parser/src/pratt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use lexer::{Span, Token};
use crate::{
ArrayPlace, IdentifierExt, Lexer, Parser, Result,
ast::{
ArrayOperator, Atom, BinaryOperator, BinaryPlaceOperator, BindingPower, Expr, ExprNode,
Getline, Place, Redirection, Ternary, UnaryOperator, UnaryPlaceOperator, Variable,
WriteKind,
ArrayOperator, Atom, BinaryOperator, BinaryPlaceOperator, BindingPower, BuiltinFunction,
Expr, ExprNode, Getline, Place, Redirection, Ternary, UnaryOperator, UnaryPlaceOperator,
Variable, WriteKind,
},
diagnostics::ParsingError,
lex::{SpanExt, TokenExt},
Expand Down Expand Up @@ -358,6 +358,13 @@ impl<'a, 'b> Pratt<'a, 'b> {
lex.span().since(anchor),
))
}
} else if next == Token::Length && !lex.peek_is(&Token::OpenParent) {
// POSIX: `length` without an argument list is `length($0)`.
Ok(Expr::node(
ExprNode::BuiltinCall(BuiltinFunction::Length, Vec::new_in(self.parser.arena)),
self.parser,
lex.span().since(anchor),
))
} else if let Some(builtin) = next.maps_to_builtin() {
self.parser
.parse_function_call::<false>(lex, |args| ExprNode::BuiltinCall(builtin, args))
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_awk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,17 @@ fn nr_counts_across_files() {
.succeeds()
.stdout_is("1 1\n2 2\n3 1\n");
}

#[test]
fn length_without_parentheses_is_length_of_record() {
ucmd()
.arg("length > 2 { print length, length / 2 } BEGIN { print length }")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also cover length(/b/) and length $1 here?
the lexer change makes sure these keep working

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in f78f1fb: { print length(/b/), length $1 } on abc def and xyz gives 1 7abc and 1 3xyz, the same as gawk 5.3.2 (length $1 is length followed by $1, concatenated).

.pipe_in("ab\nabcd\nxyz\n")
.succeeds()
.stdout_only("0\n4 2\n3 1.5\n");
ucmd()
.arg("{ print length(/b/), length $1 }")
.pipe_in("abc def\nxyz\n")
.succeeds()
.stdout_only("1 7abc\n1 3xyz\n");
}
Loading