#! perl -w use strict; my $keywords = <<'EOS'; and as assert asr begin class constraint do done downto else end exception external false for fun function functor if in include inherit initializer land lazy let lor lsl lsr lxor match method mod module mutable new object of open or private rec sig struct then to true try type val virtual when while with != # & && ' ( ) * + , - -. -> . .. : :: := :> ; ;; < <- = > >] >} ? ?? [ [< [> [| ] _ ` { {< | |] } ~ parser << <: >> $ $$ $: EOS my %charnames = ( '!' => "Exclamation", '=' => "Equals", '#' => 'Hash', '&' => 'Amp', '\'' => 'SingleQuote', '(' => 'OpenParen', ')' => 'CloseParen', '*' => 'Star', '+' => 'Plus', ',' => 'Comma', '-' => 'Minus', '.' => 'Dot', '>' => 'GreaterThan', '<' => 'LessThan', '?' => 'Question', '[' => 'OpenSquare', ']' => 'CloseSquare', '_' => 'Underscore', '`' => 'Backtick', '{' => 'OpenCurly', '}' => 'CloseCurly', '~' => 'Tilde', ':' => 'Colon', ';' => 'Semi', '|' => 'Bar', '$' => 'Dollar' ); sub namefor { my $kw = shift; my $name = ''; for my $ch (split '', $kw) { if (exists $charnames{$ch}) { $name .= $charnames{$ch}; } else { $name .= $ch; } } return $name; } my $longest_name = 0; my $longest_kw = 0; foreach my $kw (split ' ', $keywords) { if (length(namefor($kw)) > $longest_name) { $longest_name = length(namefor($kw)); } if (length($kw) > $longest_kw) { $longest_kw = length($kw); } } foreach my $kw (split ' ', $keywords) { # for the TokenType enum #print " KW_", uc(namefor($kw)), ", ", # (" " x ($longest_name - length(namefor($kw)))), # "// \"", $kw, "\"\n"; # for the keywords dictionary #print " .Add(\"", $kw, "\", ", # (' ' x ($longest_kw - length($kw))), # "TokenType.KW_", uc(namefor($kw)), ")\n"; # for the parser's token list. Seems GPPG wants to own # the token enum. print "\%token KW_", uc(namefor($kw)), " ", (" " x ($longest_name - length(namefor($kw)))), "/* \"", $kw, "\" */\n"; }