]> git.sur5r.net Git - i3/i3/blobdiff - generate-command-parser.pl
Merge branch 'release-4.16.1'
[i3/i3] / generate-command-parser.pl
index 66e44b6cd83a020924d7e7ae327d6f6eaa7e9831..052e4c66338c72076b6e7121eedeec3172d6b63f 100755 (executable)
@@ -2,7 +2,7 @@
 # vim:ts=4:sw=4:expandtab
 #
 # i3 - an improved dynamic tiling window manager
-# © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
+# © 2009 Michael Stapelberg and contributors (see also: LICENSE)
 #
 # generate-command-parser.pl: script to generate parts of the command parser
 # from its specification file parser-specs/commands.spec.
@@ -65,7 +65,7 @@ for my $line (@raw_lines) {
 my $current_state;
 
 for my $line (@lines) {
-    if (my ($state) = ($line =~ /^state ([A-Z_]+):$/)) {
+    if (my ($state) = ($line =~ /^state ([A-Z0-9_]+):$/)) {
         #say "got a new state: $state";
         $current_state = $state;
     } else {
@@ -77,21 +77,21 @@ for my $line (@lines) {
             ($line =~ /
                 ^\s*                  # skip leading whitespace
                 ([a-z_]+ \s* = \s*|)  # optional identifier
-                (.*?) -> \s*          # token 
+                (.*?) -> \s*          # token
                 (.*)                  # optional action
              /x);
 
         # Cleanup the identifier (if any).
         $identifier =~ s/^\s*(\S+)\s*=\s*$/$1/g;
 
-        # Cleanup the tokens (remove whitespace).
-        $tokens =~ s/\s*//g;
-
         # The default action is to stay in the current state.
         $action = $current_state if length($action) == 0;
 
         #say "identifier = *$identifier*, token = *$tokens*, action = *$action*";
         for my $token (split(',', $tokens)) {
+            # Cleanup trailing/leading whitespace.
+            $token =~ s/^\s*//g;
+            $token =~ s/\s*$//g;
             my $store_token = {
                 token => $token,
                 identifier => $identifier,
@@ -109,29 +109,28 @@ for my $line (@lines) {
 # Second step: Generate the enum values for all states.
 
 # It is important to keep the order the same, so we store the keys once.
-# We sort descendingly by length to be able to replace occurences of the state
+# We sort descendingly by length to be able to replace occurrences of the state
 # name even when one state’s name is included in another one’s (like FOR_WINDOW
 # is in FOR_WINDOW_COMMAND).
-my @keys = sort { length($b) <=> length($a) } keys %states;
+my @keys = sort { (length($b) <=> length($a)) or ($a cmp $b) } keys %states;
 
 open(my $enumfh, '>', "GENERATED_${prefix}_enums.h");
 
-# XXX: we might want to have a way to do this without a trailing comma, but gcc
-# seems to eat it.
 my %statenum;
 say $enumfh 'typedef enum {';
 my $cnt = 0;
 for my $state (@keys, '__CALL') {
-    say $enumfh "    $state = $cnt,";
+    say $enumfh ',' if $cnt > 0;
+    print $enumfh "    $state = $cnt";
     $statenum{$state} = $cnt;
     $cnt++;
 }
-say $enumfh '} cmdp_state;';
+say $enumfh "\n} cmdp_state;";
 close($enumfh);
 
 # Third step: Generate the call function.
 open(my $callfh, '>', "GENERATED_${prefix}_call.h");
-my $resultname = uc(substr($prefix, 0, 1)) . substr($prefix, 1) . 'Result';
+my $resultname = uc(substr($prefix, 0, 1)) . substr($prefix, 1) . 'ResultIR';
 say $callfh "static void GENERATED_call(const int call_identifier, struct $resultname *result) {";
 say $callfh '    switch (call_identifier) {';
 my $call_id = 0;
@@ -155,12 +154,20 @@ for my $state (@keys) {
         # to generate a format string. The format uses %d for <number>s,
         # literal numbers or state IDs and %s for NULL, <string>s and literal
         # strings.
+
+        # remove the function name temporarily, so that the following
+        # replacements only apply to the arguments.
+        my ($funcname) = ($fmt =~ /^(.+)\(/);
+        $fmt =~ s/^$funcname//;
+
         $fmt =~ s/$_/%d/g for @keys;
         $fmt =~ s/\$([a-z_]+)/%s/g;
         $fmt =~ s/\&([a-z_]+)/%ld/g;
         $fmt =~ s/"([a-z0-9_]+)"/%s/g;
         $fmt =~ s/(?:-?|\b)[0-9]+\b/%d/g;
 
+        $fmt = $funcname . $fmt;
+
         say $callfh "         case $call_id:";
         say $callfh "             result->next_state = $next_state;";
         say $callfh '#ifndef TEST_PARSER';
@@ -193,7 +200,6 @@ say $callfh '        default:';
 say $callfh '            printf("BUG in the parser. state = %d\n", call_identifier);';
 say $callfh '            assert(false);';
 say $callfh '    }';
-say $callfh '    state = result->next_state;';
 say $callfh '}';
 close($callfh);
 
@@ -218,7 +224,7 @@ for my $state (@keys) {
             $next_state = '__CALL';
         }
         my $identifier = $token->{identifier};
-        say $tokfh qq|    { "$token_name", "$identifier", $next_state, { $call_identifier } }, |;
+        say $tokfh qq|    { "$token_name", "$identifier", $next_state, { $call_identifier } },|;
     }
     say $tokfh '};';
 }