| 1 | # $Id: SxprParser.pm,v 1.22 2008/06/03 17:31:15 cmungall Exp $ | ||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||
| 3 | # Copyright (C) 2002 Chris Mungall <cjm@fruitfly.org> | ||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||
| 5 | # See also - http://stag.sourceforge.net | ||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||
| 7 | # This module is free software. | ||||||||||||||||||||||
| 8 | # You may distribute this module under the same terms as perl itself | ||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | package Data::Stag::SxprParser; | ||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | SxprParser.pm - parses Stag S-expression format | ||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | =head1 AUTHOR | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | 4 | ▶ | use Exporter; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 28 | 4 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 29 | 4 | ▶ | use FileHandle; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | 4 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 31 | 4 | ▶ | use Data::Stag qw(:all); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 32 | 4 | ▶ | use base qw(Data::Stag::BaseGenerator Exporter); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 4 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 35 | 4 | $VERSION="0.14"; | |||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 0 | ▶ | sub fmtstr { | ||||||||||||||||||||
|
Subroutine
fmtstr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 38 | 0 | return 'sxpr'; | |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | 7 | ▶ | sub parse_fh { | ||||||||||||||||||||
|
Subroutine
parse_fh: called CC=30 SCAR=36.9
Pod
undocumented
| |||||||||||||||||||||||
| 42 | 7 | my $self = shift; | |||||||||||||||||||||
| 43 | 7 | my $fh = shift; | |||||||||||||||||||||
| 44 | 7 | my $line_no = 0; | |||||||||||||||||||||
| 45 | 7 | my $state = "init"; | |||||||||||||||||||||
| 46 | 7 | my $txt = ''; | |||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | # c: comment ; | ||||||||||||||||||||||
| 49 | # q: quote (double quote only) | ||||||||||||||||||||||
| 50 | # o: opening tag - after round bracket, before whitespace or () | ||||||||||||||||||||||
| 51 | # 0: body, or awaiting open or close | ||||||||||||||||||||||
| 52 | 7 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 53 | 105 | $line_no++; | |||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 105 | ▶ | while (length($line)) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | 1956 | my $c = substr($line,0,1,''); | |||||||||||||||||||||
| 57 | 1956 | ▶ | if ($state eq 'init') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 58 | 11 | ▶ | if ($c eq '(') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 59 | # at start - do nothing | ||||||||||||||||||||||
| 60 | 7 | $state = 0; | |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | elsif ($c eq "'") { | ||||||||||||||||||||||
| 63 | # leading quote is allowed [list constructor in lisp] | ||||||||||||||||||||||
| 64 | # (good for editing in emacs) | ||||||||||||||||||||||
| 65 | 4 | next; | |||||||||||||||||||||
| 66 | 0 | $state = 0; | |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | elsif ($c =~ /\s/) { | ||||||||||||||||||||||
| 69 | 0 | next; | |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | elsif ($c eq ';') { | ||||||||||||||||||||||
| 72 | 0 | $state = 'c'; | |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||
| 75 | 0 | $self->throw("Line: $line_no\n$line\nExpected \"(\" at start of file"); | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | 1952 | ▶ | $state ne 'init' || $self->throw("assertion error: state=$state"); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | 1952 | ▶ | if ($state eq 'c') { # comment | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 81 | # newline is the only char that can break out of comments | ||||||||||||||||||||||
| 82 | 0 | ▶ | if ($c eq "\n") { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 83 | 0 | $state = 0; | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | 0 | next; | |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | 1952 | ▶ | $state ne 'c' || $self->throw("assertion error: state=$state"); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 1952 | ▶ | if ($state eq 'q') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 90 | 465 | ▶ | if ($c eq '"') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 91 | 62 | $state = 0; | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||
| 94 | 403 | $txt .= $c; | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | 465 | next; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | 1487 | ▶ | $state ne 'q' || $self->throw("assertion error: state=$state"); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | 1487 | ▶ | if ($c eq '"') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 101 | 62 | $state = 'q'; | |||||||||||||||||||||
| 102 | 62 | next; | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | 1425 | ▶ | $state ne 'q' || $self->throw("assertion error: state=$state"); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | 1425 | ▶ | if ($c eq ';') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 107 | # can only open comments when NOT in quotes | ||||||||||||||||||||||
| 108 | 0 | $state = 'c'; | |||||||||||||||||||||
| 109 | 0 | next; | |||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 1425 | ▶ | if ($c eq '(') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 113 | 110 | ▶ | if ($state eq 'o') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 114 | 6 | ▶ | if (!$txt) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 115 | 0 | $self->throw("Line: $line_no\b$line\ndouble open brackets (( not allowed!"); | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | 6 | $self->start_event($txt); | |||||||||||||||||||||
| 118 | 6 | $txt = ''; | |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | 110 | $state = 'o'; | |||||||||||||||||||||
| 121 | 110 | next; | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | 1315 | ▶ | if ($c eq ')') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 124 | 110 | ▶ | if ($state eq 'o') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 125 | 1 | ▶ | if (!$txt) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 126 | 0 | $self->throw("Line: $line_no\b$line\n () not allowed!"); | |||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||
| 128 | 1 | $self->start_event($txt); | |||||||||||||||||||||
| 129 | 1 | $txt = ''; | |||||||||||||||||||||
| 130 | 1 | $self->end_event; | |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||
| 133 | 109 | ▶ | if ($txt) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 134 | 66 | $self->evbody($txt); | |||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||
| 136 | 109 | $txt = ''; | |||||||||||||||||||||
| 137 | 109 | $self->end_event; | |||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||
| 139 | 110 | $state=0; | |||||||||||||||||||||
| 140 | 110 | next; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | 1205 | ▶ | if ($state eq 'o') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 143 | 759 | ▶ | if ($c =~ /\s/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 144 | # reached last char of start event name | ||||||||||||||||||||||
| 145 | 103 | $self->start_event($txt); | |||||||||||||||||||||
| 146 | 103 | $txt = ''; | |||||||||||||||||||||
| 147 | 103 | $state = 0; | |||||||||||||||||||||
| 148 | 103 | next; | |||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||
| 151 | 656 | $txt .= $c; | |||||||||||||||||||||
| 152 | 656 | next; | |||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||
| 155 | 446 | ▶ | $state == 0 || $self->throw("assertion error: state=$state"); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 156 | 446 | ▶ | if ($c =~ /\s/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 157 | 442 | next; | |||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||
| 159 | 4 | $txt .= $c; | |||||||||||||||||||||
| 160 | 4 | next; | |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||
| 163 | 7 | ▶ | if ($txt =~ /\S/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 164 | 0 | $self->throw("text at end: $txt"); | |||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||
| 167 | |||||||||||||||||||||||
| 168 | 4 | 1; | |||||||||||||||||||||