1#!/usr/bin/env perl 2 3# This script generates constants class for token type (Regexp::Lexer::TokenType) 4# from `types.yaml` 5 6use strict; 7use warnings; 8use utf8; 9use FindBin; 10use YAML::Tiny; 11 12my $yaml = YAML::Tiny->read("$FindBin::Bin/types.yaml")->[0]; 13 14my $src = <<'...'; 15package Regexp::Lexer::TokenType; 16 17# *** DO NOT EDIT THIS FILE DIRECTLY *** 18# This file is generated by 'author/generate_token_type.pl' 19 20use strict; 21use warnings; 22use utf8; 23 24use constant { 25... 26 27my $type_value = 1; 28 29my $normal_types = $yaml->{normal}; 30for my $normal_type (@$normal_types) { 31 $src .= <<"..."; 32 $normal_type => { 33 id => $type_value, 34 name => '$normal_type', 35 }, 36... 37 $type_value++; 38}; 39 40my $escaped_types = $yaml->{escaped}; 41for my $escaped_type (@$escaped_types) { 42 $src .= <<"..."; 43 Escaped$escaped_type => { 44 id => $type_value, 45 name => 'Escaped$escaped_type', 46 }, 47... 48 $type_value++; 49} 50 51$src .= <<'...'; 52}; 53 541; 55__END__ 56 57=for stopwords EscapedCChar 58 59=encoding utf-8 60 61=head1 NAME 62 63Regexp::Lexer::TokenType - Token types of Regexp::Lexer 64 65=head1 DESCRIPTION 66 67This module provides token types for L<Regexp::Lexer>. 68 69Format of token type is bellow; 70 71 { 72 id => <ID of token>, 73 name => <name of token> 74 } 75 76If you want to identify the token, I highly recommend you to use C<id>. 77 78=head1 TYPES 79 80=over 4 81 82... 83 84for my $normal_type (@$normal_types) { 85 $src .= "=item * $normal_type\n\n"; 86} 87 88for my $escaped_type (@$escaped_types) { 89 $src .= "=item * Escaped$escaped_type\n\n"; 90} 91 92$src .= <<'...'; 93=back 94 95=head1 LICENSE 96 97Copyright (C) moznion. 98 99This library is free software; you can redistribute it and/or modify 100it under the same terms as Perl itself. 101 102=head1 AUTHOR 103 104moznion E<lt>moznion@gmail.comE<gt> 105 106=cut 107 108... 109 110my $package_file = "$FindBin::Bin/../lib/Regexp/Lexer/TokenType.pm"; 111 112open my $fh, '>', $package_file; 113print ${fh} $src; 114 115print "Generated!\n"; 116 117__END__ 118 119