generate-command-parser: make input/output configurable

This commit is contained in:
Michael Stapelberg 2012-10-07 16:30:34 +02:00
parent 69a77d182e
commit d36264e403
3 changed files with 19 additions and 11 deletions

View File

@ -12,8 +12,18 @@
use strict; use strict;
use warnings; use warnings;
use Data::Dumper; use Data::Dumper;
use Getopt::Long;
use v5.10; use v5.10;
my $input = '';
my $prefix = '';
my $result = GetOptions(
'input=s' => \$input,
'prefix=s' => \$prefix
);
die qq|Input file "$input" does not exist!| unless -e $input;
# reads in a whole file # reads in a whole file
sub slurp { sub slurp {
open my $fh, '<', shift; open my $fh, '<', shift;
@ -24,8 +34,6 @@ sub slurp {
# Stores the different states. # Stores the different states.
my %states; my %states;
# XXX: dont hardcode input and output
my $input = '../parser-specs/commands.spec';
my @raw_lines = split("\n", slurp($input)); my @raw_lines = split("\n", slurp($input));
my @lines; my @lines;
@ -103,7 +111,7 @@ for my $line (@lines) {
# It is important to keep the order the same, so we store the keys once. # It is important to keep the order the same, so we store the keys once.
my @keys = keys %states; my @keys = keys %states;
open(my $enumfh, '>', 'GENERATED_enums.h'); open(my $enumfh, '>', "GENERATED_${prefix}_enums.h");
# XXX: we might want to have a way to do this without a trailing comma, but gcc # XXX: we might want to have a way to do this without a trailing comma, but gcc
# seems to eat it. # seems to eat it.
@ -117,7 +125,7 @@ say $enumfh '} cmdp_state;';
close($enumfh); close($enumfh);
# Third step: Generate the call function. # Third step: Generate the call function.
open(my $callfh, '>', 'GENERATED_call.h'); open(my $callfh, '>', "GENERATED_${prefix}_call.h");
say $callfh 'static void GENERATED_call(const int call_identifier, struct CommandResult *result) {'; say $callfh 'static void GENERATED_call(const int call_identifier, struct CommandResult *result) {';
say $callfh ' switch (call_identifier) {'; say $callfh ' switch (call_identifier) {';
my $call_id = 0; my $call_id = 0;
@ -168,11 +176,11 @@ close($callfh);
# Fourth step: Generate the token datastructures. # Fourth step: Generate the token datastructures.
open(my $tokfh, '>', 'GENERATED_tokens.h'); open(my $tokfh, '>', "GENERATED_${prefix}_tokens.h");
for my $state (@keys) { for my $state (@keys) {
my $tokens = $states{$state}; my $tokens = $states{$state};
say $tokfh 'cmdp_token tokens_' . $state . '[' . scalar @$tokens . '] = {'; say $tokfh 'static cmdp_token tokens_' . $state . '[' . scalar @$tokens . '] = {';
for my $token (@$tokens) { for my $token (@$tokens) {
my $call_identifier = 0; my $call_identifier = 0;
my $token_name = $token->{token}; my $token_name = $token->{token};
@ -192,7 +200,7 @@ for my $state (@keys) {
say $tokfh '};'; say $tokfh '};';
} }
say $tokfh 'cmdp_token_ptr tokens[' . scalar @keys . '] = {'; say $tokfh 'static cmdp_token_ptr tokens[' . scalar @keys . '] = {';
for my $state (@keys) { for my $state (@keys) {
my $tokens = $states{$state}; my $tokens = $states{$state};
say $tokfh ' { tokens_' . $state . ', ' . scalar @$tokens . ' },'; say $tokfh ' { tokens_' . $state . ', ' . scalar @$tokens . ' },';

View File

@ -46,7 +46,7 @@
* input parser-specs/commands.spec. * input parser-specs/commands.spec.
******************************************************************************/ ******************************************************************************/
#include "GENERATED_enums.h" #include "GENERATED_commands_enums.h"
typedef struct token { typedef struct token {
char *name; char *name;
@ -63,7 +63,7 @@ typedef struct tokenptr {
int n; int n;
} cmdp_token_ptr; } cmdp_token_ptr;
#include "GENERATED_tokens.h" #include "GENERATED_commands_tokens.h"
/******************************************************************************* /*******************************************************************************
* The (small) stack where identified literals are stored during the parsing * The (small) stack where identified literals are stored during the parsing
@ -182,7 +182,7 @@ static Match current_match;
static struct CommandResult subcommand_output; static struct CommandResult subcommand_output;
static struct CommandResult command_output; static struct CommandResult command_output;
#include "GENERATED_call.h" #include "GENERATED_commands_call.h"
static void next_state(const cmdp_token *token) { static void next_state(const cmdp_token *token) {

View File

@ -55,7 +55,7 @@ src/commands_parser.o: src/commands_parser.c $(i3_HEADERS_DEP) i3-command-parser
i3-command-parser.stamp: generate-command-parser.pl parser-specs/commands.spec i3-command-parser.stamp: generate-command-parser.pl parser-specs/commands.spec
echo "[i3] Generating command parser" echo "[i3] Generating command parser"
(cd include; ../generate-command-parser.pl) (cd include; ../generate-command-parser.pl --input=../parser-specs/commands.spec --prefix=commands)
touch $@ touch $@
i3: libi3.a $(i3_OBJECTS) i3: libi3.a $(i3_OBJECTS)