| 1 | use v5.10scip-perl cpan Regexp-Grammars 1.058 `v5.10`/; |
| 2 | use warningsscip-perl cpan Regexp-Grammars 1.058 warnings/; |
| 3 | |
| 4 | my $parser = do{ |
| 5 | use Regexp::Grammarsscip-perl cpan Regexp-Grammars 1.058 Regexp/Grammars/; |
| 6 | qr{ |
| 7 | <debug:on> |
| 8 | <file> |
| 9 | |
| 10 | <objrule: Latex::file> |
| 11 | <[element]>* |
| 12 | |
| 13 | <objrule: Latex::element> |
| 14 | <special> | <command> | <literal> |
| 15 | |
| 16 | <token: special> |
| 17 | <MATCH= mathematical> |
| 18 | | <MATCH= group> |
| 19 | | <MATCH= comment> |
| 20 | | <MATCH= vinculum> |
| 21 | | <MATCH= newline> |
| 22 | | <MATCH= super> |
| 23 | | <MATCH= sub> |
| 24 | | <MATCH= column_separator> |
| 25 | |
| 26 | <objrule: Latex::mathematical> |
| 27 | \$ <[element]>* \$ |
| 28 | |
| 29 | <objrule: comment> |
| 30 | \# <MATCH= ( [^\n]* \n [^\S\n]* )> |
| 31 | |
| 32 | <objrule: Latex::vinculum> |
| 33 | \~ |
| 34 | |
| 35 | <objrule: Latex::newline> |
| 36 | \\ \\ |
| 37 | |
| 38 | <objrule: Latex::super> |
| 39 | \^ |
| 40 | |
| 41 | <objrule: Latex::sub> |
| 42 | \_ |
| 43 | |
| 44 | <objrule: Latex::column_separator> |
| 45 | \$ |
| 46 | |
| 47 | <objrule: Latex::command> |
| 48 | \\ <name> <options>? <[arg=group]>* |
| 49 | |
| 50 | <objrule: Latex::options> |
| 51 | \[ <[option]>+ % <_Sep=(,)> \] |
| 52 | |
| 53 | <objrule: Latex::group> |
| 54 | \{ <[element]>* \} |
| 55 | |
| 56 | <objrule: Latex::option> |
| 57 | [^][\$&%#_{}~^\s,]++ |
| 58 | |
| 59 | <objtoken: Latex::literal> |
| 60 | ( [^][\\\$&%#_{}~^\s]*+ (?: \\[^\w\\] [^][\\\$&%#_{}~^\s]*+ )*+ ) |
| 61 | <require: (?{ length($CAPTURE) > 0 })> |
| 62 | |
| 63 | <token: name> |
| 64 | <.alphas> | <.single_nonalpha> |
| 65 | |
| 66 | <token: alphas> |
| 67 | [^\W\d_]++ |
| 68 | |
| 69 | <token: single_nonalpha> |
| 70 | [\W\d_] |
| 71 | |
| 72 | }xms |
| 73 | }; |
| 74 | |
| 75 | my $input = do{ local $/scip-perl cpan Regexp-Grammars 1.058 main/`$/`.; <DATA>}; |
| 76 | if ($input =~ $parser) { |
| 77 | $/scip-perl cpan Regexp-Grammars 1.058 main/`%/`.{file}->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().(0); |
| 78 | } |
| 79 | |
| 80 | sub Latex::file::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::file::explain`(). |
| 81 | { |
| 82 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 83 | for my $element (@{$self->{element}}) |
| 84 | { |
| 85 | $element->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level); |
| 86 | printscip-perl cpan Regexp-Grammars 1.058 main/print(). "\n"; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | sub Latex::element::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::element::explain`(). |
| 91 | { |
| 92 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 93 | ( $self->{command} |
| 94 | || $self->{literal} |
| 95 | || $self->{special} )->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level) |
| 96 | } |
| 97 | |
| 98 | sub Latex::command::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::command::explain`(). |
| 99 | { |
| 100 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 101 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Command:"; |
| 102 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x($level+1), "Name: $self->{name}"; |
| 103 | if ($self->{options}) { |
| 104 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "\tOptions:"; |
| 105 | $self->{options}->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level+2) |
| 106 | } |
| 107 | |
| 108 | for my $arg (@{$self->{arg}}) { |
| 109 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "\tArg:"; |
| 110 | $arg->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level+2) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | sub Latex::options::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::options::explain`(). |
| 115 | { |
| 116 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 117 | $_scip-perl cpan Regexp-Grammars 1.058 main/$_.->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level) foreach @{$self->{option}}; |
| 118 | } |
| 119 | |
| 120 | sub Latex::group::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::group::explain`(). |
| 121 | { |
| 122 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 123 | $_scip-perl cpan Regexp-Grammars 1.058 main/$_.->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level) foreach @{$self->{element}}; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | sub Latex::option::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::option::explain`(). |
| 128 | { |
| 129 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 130 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Option: $self->{q{}}"; |
| 131 | } |
| 132 | |
| 133 | sub Latex::literal::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::literal::explain`(). |
| 134 | { |
| 135 | my ($self, $level, $label) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 136 | $label //= 'Literal'; |
| 137 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "$label: ", $self->{q{}}; |
| 138 | } |
| 139 | |
| 140 | sub Latex::mathematical::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::mathematical::explain`(). { |
| 141 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 142 | |
| 143 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Mathematical:"; |
| 144 | for my $element (@{$self->{element}}) |
| 145 | { |
| 146 | $element->explainscip-perl cpan Regexp-Grammars 1.058 main/explain().($level+1); |
| 147 | printscip-perl cpan Regexp-Grammars 1.058 main/print(). "\n"; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | sub Latex::comment::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::comment::explain`(). { |
| 152 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 153 | |
| 154 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Comment: $self->{q{}}"; |
| 155 | } |
| 156 | |
| 157 | sub Latex::vinculum::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::vinculum::explain`(). { |
| 158 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 159 | |
| 160 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Non-breaking space"; |
| 161 | } |
| 162 | |
| 163 | sub Latex::newline::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::newline::explain`(). { |
| 164 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 165 | |
| 166 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Newline"; |
| 167 | } |
| 168 | |
| 169 | sub Latex::super::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::super::explain`(). { |
| 170 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 171 | |
| 172 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Superscript..."; |
| 173 | } |
| 174 | |
| 175 | sub Latex::sub::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::sub::explain`(). { |
| 176 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 177 | |
| 178 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Subscript..."; |
| 179 | } |
| 180 | |
| 181 | sub Latex::column_separator::explainscip-perl cpan Regexp-Grammars 1.058 main/`Latex::column_separator::explain`(). { |
| 182 | my ($self, $level) = @_scip-perl cpan Regexp-Grammars 1.058 main/`@_`.; |
| 183 | |
| 184 | sayscip-perl cpan Regexp-Grammars 1.058 main/say(). "\t"x$level, "Column Break"; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | __DATA__ |
| 189 | \documentclass[a4paper,11pt]{article} |
| 190 | \usepackage{latexsym} |
| 191 | \author{D.~Conway} |
| 192 | \title{Parsing \LaTeX{}} |
| 193 | \begin{document} |
| 194 | \maketitle |
| 195 | \tableofcontents |
| 196 | \section{Description} |
| 197 | ...is easy\footnote{But not\\ \emph{necessarily} simple}. |
| 198 | In fact it's $easy_peasy^2$ to do. |
| 199 | \end{document} |