From 3177fb7abb48479332cce56b02b6407968fb2d27 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 28 Dec 2019 00:36:50 +0000 Subject: [PATCH] Macro assistance part 1 - macro directive Define the &:macro directive, which is a dollar-doubled version of make's own `define'. We must introuce a new concept of `nesting', for when "the content of the construct is $-doubled" in the words of the README. The implementation is in the main process_input_mk function: it is a multi-line scope of some kind with an ad-hoc ending condition, and its own dollar-doubling setting. We will also want a convenient syntax for $(eval $(call...)), which we're going to introduce in a moment. Signed-off-by: Ian Jackson --- README | 9 +++++++++ generate | 21 ++++++++++++++++++++- tests/filter/extract-doctest | 2 ++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README b/README index d750cb8..130a1c7 100644 --- a/README +++ b/README @@ -331,6 +331,15 @@ Dollar doubling and macro assistance &$- Stops dollar-doubling Both are idempotent and local to the file or context. +Sometimes we will show $'s being doubled inside another construct. +This means the content of the construct is $-doubled: $-doubling is +locally enabled, and restored afterwards. + +&:macro NAME => define NAME +STUFF $ THINGS .. STUFF $$ THINGS +&:endm .. endef + NAME is processed for & + While dollar-doubling: - - - - - - - - - - - diff --git a/generate b/generate index ec47866..3ccfa0d 100755 --- a/generate +++ b/generate @@ -180,13 +180,29 @@ sub process_input_mk ($$$$) { $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap; local $ddbl; + my @nest; + + my $push_nest = sub { + my ($nk, $nndbl) = @_; + unshift @nest, [ $nk, $ddbl ]; + $ddbl = $nndbl; + }; + my $pop_nest = sub { + my ($nk) = @_; + die unless $nest[0][0] eq $nk; + $ddbl = (shift @nest)[1]; + }; while (<$input>) { if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) { $$esclitr = $1; $set_esc->(); next; - } elsif (s#^\s*$esc\:(?=(-?)include)##) { + } elsif (s#^\s*$esc\:endm\s+$##) { + $pop_nest->('Macro'); + od "endef\n"; + next; + } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) { $buffering_output=''; } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) { die "unknown directive $1"; @@ -238,6 +254,9 @@ sub process_input_mk ($$$$) { my $subf = "$srcdir/$2"; process_input_mk($targets, $subf, $esclitr, $1); od "\n"; + } elsif (m#^macro\s+(\S+)\s+$#) { + od "define $1\n"; + $push_nest->('Macro', 1); } else { die "internal error buffering directive $_ "; } diff --git a/tests/filter/extract-doctest b/tests/filter/extract-doctest index 35870ce..57f5313 100755 --- a/tests/filter/extract-doctest +++ b/tests/filter/extract-doctest @@ -138,6 +138,8 @@ sub writeout ($) { 'dollar doubling', sub { my ($e) = @_; + # adhoc: skip &:macro in already-doubling part + return 0 if $e->{In} =~ m{^\&\:macro}; return 0 if $e->{CQ}; return $e->{DD} || !grep { # If there are two entries with the same In, -- 2.30.2