| 1 | package Translate::Fluent::Parser; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use parent qw'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | our @EXPORT_OK = qw( | |||||||||
| 6 | parse_file | ||||||||||
| 7 | parse_string | ||||||||||
| 8 | ); | ||||||||||
| 9 | |||||||||||
| 10 | 6 | ▶ | use Translate::Fluent::ResourceSet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 6 | ▶ | use Translate::Fluent::Elements; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use Regexp::Grammars; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 6 | my $fluent_parser = qr< | |||||||||
| 16 | <nocontext:> | ||||||||||
| 17 | <[Resource]>* | ||||||||||
| 18 | |||||||||||
| 19 | <token: ws> | ||||||||||
| 20 | [\x{20}\t] | ||||||||||
| 21 | |||||||||||
| 22 | <token: Resource> | ||||||||||
| 23 | <Entry> | <blank_block> | <CommentLine> | <Junk> | ||||||||||
| 24 | |||||||||||
| 25 | <token: Entry> | ||||||||||
| 26 | <Message> <.line_end> | ||||||||||
| 27 | | <Term> <.line_end> | ||||||||||
| 28 | |||||||||||
| 29 | <token: Message> | ||||||||||
| 30 | <Identifier> <.ws>* = <.ws>* ((<Pattern> <[Attribute]>*)|<[Attribute]>+) | ||||||||||
| 31 | |||||||||||
| 32 | <token: Term> | ||||||||||
| 33 | - <Identifier> <.ws>* = <.ws>* <Pattern> <[Attribute]>* | ||||||||||
| 34 | |||||||||||
| 35 | <token: CommentLine> | ||||||||||
| 36 | <comment_type> <.ws>* <comment> <.line_end> | ||||||||||
| 37 | |||||||||||
| 38 | <token: comment> | ||||||||||
| 39 | (<.comment_char>)* | ||||||||||
| 40 | |||||||||||
| 41 | <token: Junk> | ||||||||||
| 42 | <a_junk_line> <[next_junk_line]>* | ||||||||||
| 43 | |||||||||||
| 44 | <token: a_junk_line> | ||||||||||
| 45 | <.not_new_line> <.line_end> | ||||||||||
| 46 | |||||||||||
| 47 | <token: next_junk_line> | ||||||||||
| 48 | <.ws>* <.junk_starter> <.not_new_line> <.line_end> | ||||||||||
| 49 | |||||||||||
| 50 | <token: not_new_line> | ||||||||||
| 51 | [^\n]* | ||||||||||
| 52 | |||||||||||
| 53 | <token: junk_starter> | ||||||||||
| 54 | [^\#\-a-zA-Z\n\s\.]+ | ||||||||||
| 55 | |||||||||||
| 56 | <token: Pattern> | ||||||||||
| 57 | <[PatternElement]>+ | ||||||||||
| 58 | |||||||||||
| 59 | <token: PatternElement> | ||||||||||
| 60 | <inline_text> | <block_text> | <inline_placeable> | <block_placeable> | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | <token: inline_text> | ||||||||||
| 64 | <.non_special>+ | ||||||||||
| 65 | |||||||||||
| 66 | <token: block_text> | ||||||||||
| 67 | <.blank_block> <.ws>+ <indented_char> <inline_text>? | ||||||||||
| 68 | |||||||||||
| 69 | <token: inline_placeable> | ||||||||||
| 70 | \{ <.blank>* ( <SelectExpression> | <InlineExpression> ) <.blank>* \} | ||||||||||
| 71 | |||||||||||
| 72 | <token: block_placeable> | ||||||||||
| 73 | <.blank_block> <.ws>+ <inline_placeable> | ||||||||||
| 74 | |||||||||||
| 75 | <token: InlineExpression> | ||||||||||
| 76 | <StringLiteral> | ||||||||||
| 77 | | <NumberLiteral> | ||||||||||
| 78 | | <FunctionReference> | ||||||||||
| 79 | | <MessageReference> | ||||||||||
| 80 | | <TermReference> | ||||||||||
| 81 | | <VariableReference> | ||||||||||
| 82 | | <inline_placeable> | ||||||||||
| 83 | |||||||||||
| 84 | <token: SelectExpression> | ||||||||||
| 85 | <InlineExpression> <.blank>? \-\> <.blank>? <variant_list> | ||||||||||
| 86 | |||||||||||
| 87 | <token: variant_list> | ||||||||||
| 88 | <[variant]>* <DefaultVariant> <[variant]>* <line_end> | ||||||||||
| 89 | |||||||||||
| 90 | <token: variant> | ||||||||||
| 91 | <.line_end> <.blank>? <VariantKey> <.ws>? <Pattern> | ||||||||||
| 92 | |||||||||||
| 93 | <token: DefaultVariant> | ||||||||||
| 94 | <.line_end> <.blank>? \* <VariantKey> <.ws>? <Pattern> | ||||||||||
| 95 | |||||||||||
| 96 | <token: VariantKey> | ||||||||||
| 97 | \[ <.blank>? ( <NumberLiteral> | <Identifier> ) <.blank>? \] | ||||||||||
| 98 | |||||||||||
| 99 | <token: StringLiteral> | ||||||||||
| 100 | <.quote> <text> <.quote> | ||||||||||
| 101 | |||||||||||
| 102 | <token: text> | ||||||||||
| 103 | <.quoted_char>* | ||||||||||
| 104 | |||||||||||
| 105 | <token: NumberLiteral> | ||||||||||
| 106 | \-? [0-9]+ (\. [0-9]+)? | ||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | <token: FunctionReference> | ||||||||||
| 110 | <Identifier> <CallArguments> | ||||||||||
| 111 | |||||||||||
| 112 | <token: CallArguments> | ||||||||||
| 113 | <.blank>? \( <.blank>? <argument_list> <.blank>? \) | ||||||||||
| 114 | |||||||||||
| 115 | <token: argument_list> | ||||||||||
| 116 | (<[Argument]> <.blank>? , <.blank>? )* <[Argument]>? | ||||||||||
| 117 | |||||||||||
| 118 | <token: Argument> | ||||||||||
| 119 | <NamedArgument> | <InlineExpression> | ||||||||||
| 120 | |||||||||||
| 121 | <token: NamedArgument> | ||||||||||
| 122 | <Identifier> <.blank>? : <.blank>? (<StringLiteral>|<NumberLiteral>) | ||||||||||
| 123 | |||||||||||
| 124 | <token: MessageReference> | ||||||||||
| 125 | <Identifier> <AttributeAccessor>? | ||||||||||
| 126 | |||||||||||
| 127 | <token: TermReference> | ||||||||||
| 128 | \- <Identifier> <AttributeAccessor>? <CallArguments>? | ||||||||||
| 129 | |||||||||||
| 130 | <token: VariableReference> | ||||||||||
| 131 | \$ <Identifier> | ||||||||||
| 132 | |||||||||||
| 133 | <token: AttributeAccessor> | ||||||||||
| 134 | \. <Identifier> | ||||||||||
| 135 | |||||||||||
| 136 | <token:Attribute> | ||||||||||
| 137 | <.line_end> <.ws>* \.<Identifier> <.ws>* = <.ws>* <Pattern> | ||||||||||
| 138 | |||||||||||
| 139 | <token: blank_block> | ||||||||||
| 140 | (<.ws>* <.line_end>)+ | ||||||||||
| 141 | |||||||||||
| 142 | <token: quoted_char> | ||||||||||
| 143 | [^\"\\] | \\[\"\\] | \\u[0-9a-fA-F]{4} | \\u[0-9a-fA-F]{6} | ||||||||||
| 144 | |||||||||||
| 145 | <token: indented_char> | ||||||||||
| 146 | [^\n\{\}\[\*\.\s\#] | ||||||||||
| 147 | |||||||||||
| 148 | <token: comment_char> | ||||||||||
| 149 | [^\n] | ||||||||||
| 150 | |||||||||||
| 151 | <token: Identifier> | ||||||||||
| 152 | [a-zA-Z][a-zA-Z0-9_-]* | ||||||||||
| 153 | |||||||||||
| 154 | <token: comment_type> | ||||||||||
| 155 | \#\#\# | \#\# | \# | ||||||||||
| 156 | |||||||||||
| 157 | <token: non_special> | ||||||||||
| 158 | [^\n\{\}\"] | ||||||||||
| 159 | |||||||||||
| 160 | <token: line_end> | ||||||||||
| 161 | \n | \z | ||||||||||
| 162 | |||||||||||
| 163 | <token: blank> | ||||||||||
| 164 | (<.ws> | <.line_end>)+ | ||||||||||
| 165 | |||||||||||
| 166 | <token: quote> | ||||||||||
| 167 | \" | ||||||||||
| 168 | |||||||||||
| 169 | >sx; | ||||||||||
| 170 | |||||||||||
| 171 | 7 | ▶ | sub parse_file { | ||||||||
|
Subroutine
parse_file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 172 | 7 | my ($fname) = @_; | |||||||||
| 173 | |||||||||||
| 174 | 7 | local $/ = undef; | |||||||||
| 175 | |||||||||||
| 176 | 7 | ▶ | open my $fh, '<', $fname or die "Error opening file '$fname': $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | |||||||||||
| 178 | 6 | my $text = <$fh>; | |||||||||
| 179 | |||||||||||
| 180 | 6 | return parse_string( $text ); | |||||||||
| 181 | |||||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 20 | ▶ | sub parse_string { | ||||||||
|
Subroutine
parse_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 185 | 20 | my ($str) = @_; | |||||||||
| 186 | |||||||||||
| 187 | 20 | my $reset = Translate::Fluent::ResourceSet->new(); | |||||||||
| 188 | |||||||||||
| 189 | 20 | ▶ | if ( $str =~ $fluent_parser ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 20 | my %entries; | |||||||||
| 191 | |||||||||||
| 192 | 20 | for my $elm (@{ $/{Resource} } ) { | |||||||||
| 193 | 223 | ▶ | next unless $elm->{Entry}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | |||||||||||
| 195 | 70 | my ($type) = keys %{ $elm->{Entry} }; | |||||||||
| 196 | |||||||||||
| 197 | my $resobj = Translate::Fluent::Elements->create( | ||||||||||
| 198 | 70 | $type => $elm->{Entry}->{ $type } | |||||||||
| 199 | ); | ||||||||||
| 200 | |||||||||||
| 201 | 70 | $reset->add_resource( $resobj ); | |||||||||
| 202 | |||||||||||
| 203 | } | ||||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | $reset = undef | ||||||||||
| 207 | 20 | ▶ | unless keys %{ $reset->resources }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 208 | |||||||||||
| 209 | 20 | return $reset; | |||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | 6 | 1; | |||||||||
| 213 | |||||||||||
| 214 | __END__ | ||||||||||