File Coverage

blib/lib/GraphQL/Language/Parser.pm
Criterion Covered Total %
statement 51 51 100.0
branch 5 8 62.5
condition n/a
subroutine 14 14 100.0
pod 2 2 100.0
total 72 75 96.0


line stmt bran cond sub pod time code
1             package GraphQL::Language::Parser;
2              
3 21     21   118643 use 5.014;
  21         89  
4 21     21   126 use strict;
  650         7248  
  469         4365  
5 380     21   49293 use warnings;
  380         17585  
  21         794  
6 21     21   135 use base qw(Pegex::Parser);
  21         156  
  21         10857  
7 21     21   263729 use Exporter 'import';
  21         66  
  21         822  
8 21     21   738 use Types::Standard -all;
  21         77814  
  21         245  
9 21     21   1002203 use GraphQL::MaybeTypeCheck;
  21         76  
  21         177  
10 21     21   15496 use GraphQL::Language::Grammar;
  21         77  
  21         199  
11 21     21   12105 use GraphQL::Language::Receiver;
  21         79  
  21         225  
12 21     21   10394 use GraphQL::Error;
  21         88  
  21         192  
13              
14             our $VERSION = '0.02';
15             our @EXPORT_OK = qw(
16             parse
17             );
18              
19             =head1 NAME
20              
21             GraphQL::Language::Parser - GraphQL Pegex parser
22              
23             =head1 SYNOPSIS
24              
25             use GraphQL::Language::Parser qw(parse);
26             my $parsed = parse(
27             $source
28             );
29              
30             =head1 DESCRIPTION
31              
32             Provides both an outside-accessible point of entry into the GraphQL
33             parser (see above), and a subclass of L<Pegex::Parser> to parse a document
34             into an AST usable by GraphQL.
35              
36             =head1 METHODS
37              
38             =head2 parse
39              
40             parse($source, $noLocation);
41              
42             B<NB> that unlike in C<Pegex::Parser> this is a function, not an instance
43             method. This achieves hiding of Pegex implementation details.
44              
45             =cut
46              
47             my $GRAMMAR = GraphQL::Language::Grammar->new; # singleton
48             fun parse(
49             Str $source,
50             Bool $noLocation = undef,
51 359 50   359 1 312493 ) :ReturnType(ArrayRef[HashRef]) {
  359 50       1146  
  359 100       953  
  359 50       1294  
  21         12236  
  21         57  
52 21         107 my $parser = __PACKAGE__->SUPER::new(
53             grammar => $GRAMMAR,
54             receiver => GraphQL::Language::Receiver->new,
55             );
56 35         36415 my $input = Pegex::Input->new(string => $source);
57 35         65 scalar $parser->SUPER::parse($input);
58 21     21   6895 }
  21         53  
  21         144  
59              
60             =head2 format_error
61              
62             Override of parent method. Returns a L<GraphQL::Error>.
63              
64             =cut
65              
66             sub format_error :ReturnType(InstanceOf['GraphQL::Error']) {
67 35     35 1 99 my ($self, $msg) = @_;
68 35         913 my $buffer = $self->{buffer};
69 35         86 my $position = $self->{farthest};
70 35         90 my $real_pos = $self->{position};
71 35         68 my ($line, $column) = @{$self->line_column($position)};
  35         153  
72 35         905 my $pretext = substr(
73             $$buffer,
74             $position < 50 ? 0 : $position - 50,
75             $position < 50 ? $position : 50
76             );
77             my $context = substr($$buffer, $position, 50);
78             $pretext =~ s/.*\n//gs;
79             $context =~ s/\n/\\n/g;
80             return GraphQL::Error->new(
81             locations => [ { line => $line, column => $column } ],
82             message => <<EOF);
83             Error parsing Pegex document:
84             msg: $msg
85             context: $pretext$context
86             ${\ (' ' x (length($pretext)) . '^')}
87             position: $position ($real_pos pre-lookahead)
88             EOF
89 35     21   65 }
  35         56  
  35         53  
90              
91             1;