|
| 1 | +#!/usr/bin/perl |
| 2 | +# $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/check_rules.pl,v 1.1 2009/11/27 10:00:40 meskes Exp $ |
| 3 | +# test parser generater for ecpg |
| 4 | +# call with backend parser as stdin |
| 5 | +# |
| 6 | +# Copyright (c) 2009, PostgreSQL Global Development Group |
| 7 | +# |
| 8 | +# Written by Michael Meskes <meskes@postgresql.org> |
| 9 | +# |
| 10 | +# Placed under the same license as PostgreSQL. |
| 11 | +# |
| 12 | + |
| 13 | +if (@ARGV) { |
| 14 | +$path =$ARGV[0]; |
| 15 | +$parser =$ARGV[1]; |
| 16 | +} |
| 17 | + |
| 18 | +$[ = 1;# set array base to 1 |
| 19 | + |
| 20 | +if ($patheq'') {$path ="."; } |
| 21 | +$filename =$path ."/ecpg.addons"; |
| 22 | + |
| 23 | +if ($parsereq'') {$parser ="../../../backend/parser/gram.y"; } |
| 24 | + |
| 25 | +$replace_line{'ExecuteStmtEXECUTEnameexecute_param_clause'} ='EXECUTE prepared_name execute_param_clause execute_rest'; |
| 26 | +$replace_line{'ExecuteStmtCREATEOptTempTABLEcreate_as_targetASEXECUTEnameexecute_param_clause'} ='CREATE OptTemp TABLE create_as_target AS EXECUTE prepared_name execute_param_clause'; |
| 27 | +$replace_line{'PrepareStmtPREPAREnameprep_type_clauseASPreparableStmt'} ='PREPARE prepared_name prep_type_clause AS PreparableStmt'; |
| 28 | + |
| 29 | +$block =''; |
| 30 | +$ret = 0; |
| 31 | +$yaccmod = 0; |
| 32 | +$brace_indent = 0; |
| 33 | + |
| 34 | +open GRAM,$parserordie$!; |
| 35 | +while (<GRAM>) { |
| 36 | +chomp;# strip record separator |
| 37 | + |
| 38 | +if (/^%%/) { |
| 39 | +$yaccmode++; |
| 40 | +} |
| 41 | + |
| 42 | +if ($yaccmode != 1) { |
| 43 | +next; |
| 44 | + } |
| 45 | + |
| 46 | +$S =$_; |
| 47 | +$prec = 0; |
| 48 | + |
| 49 | +# Make sure any braces are split |
| 50 | +$S =~s/{/ {/g; |
| 51 | +$S =~s/}/ }/g; |
| 52 | +# Any comments are split |
| 53 | +$S =~s#[/][*]# /*#g; |
| 54 | +$S =~s#[*][/]# */#g; |
| 55 | + |
| 56 | +# Now split the line into individual fields |
| 57 | +$n = (@arr =split('',$S)); |
| 58 | + |
| 59 | +# Go through each field in turn |
| 60 | +for ($fieldIndexer = 1;$fieldIndexer <=$n;$fieldIndexer++) { |
| 61 | +if ($arr[$fieldIndexer]eq'*/' &&$comment) { |
| 62 | +$comment = 0; |
| 63 | +next; |
| 64 | +} |
| 65 | +elsif ($comment) { |
| 66 | +next; |
| 67 | +} |
| 68 | +elsif ($arr[$fieldIndexer]eq'/*') { |
| 69 | +# start of a multiline comment |
| 70 | +$comment = 1; |
| 71 | +next; |
| 72 | +} |
| 73 | +elsif ($arr[$fieldIndexer]eq'//') { |
| 74 | +next; |
| 75 | +} |
| 76 | +elsif ($arr[$fieldIndexer]eq'}') { |
| 77 | +$brace_indent--; |
| 78 | +next; |
| 79 | +} |
| 80 | +elsif ($arr[$fieldIndexer]eq'{') { |
| 81 | +$brace_indent++; |
| 82 | +next; |
| 83 | +} |
| 84 | + |
| 85 | +if ($brace_indent > 0) { |
| 86 | +next; |
| 87 | +} |
| 88 | + |
| 89 | +if ($arr[$fieldIndexer]eq';' ||$arr[$fieldIndexer]eq'|') { |
| 90 | +$block =$non_term_id .$block; |
| 91 | +if ($replace_line{$block}) { |
| 92 | +$block = &generate_block($replace_line{$block}); |
| 93 | +} |
| 94 | +$found{$block} ='found'; |
| 95 | +$block =''; |
| 96 | +} |
| 97 | +elsif (($arr[$fieldIndexer] =~'[A-Za-z0-9]+:') ||$arr[$fieldIndexer + 1]eq':') { |
| 98 | +$non_term_id =$arr[$fieldIndexer]; |
| 99 | +$non_term_id =~s/://g; |
| 100 | +} |
| 101 | +else { |
| 102 | +$block =$block .$arr[$fieldIndexer]; |
| 103 | +} |
| 104 | +} |
| 105 | +} |
| 106 | + |
| 107 | +close GRAM; |
| 108 | + |
| 109 | +open ECPG,$filenameordie$!; |
| 110 | + |
| 111 | +line:while (<ECPG>) { |
| 112 | +chomp;# strip record separator |
| 113 | +@Fld =split('',$_, -1); |
| 114 | + |
| 115 | +if (!/^ECPG:/) { |
| 116 | +next line; |
| 117 | + } |
| 118 | + |
| 119 | +if ($found{$Fld[2]}ne'found') { |
| 120 | +printf$Fld[2] ." is not used for building parser!\n"; |
| 121 | +$ret = 1; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +close ECPG; |
| 126 | + |
| 127 | +exit$ret; |
| 128 | + |
| 129 | +subgenerate_block { |
| 130 | +local($line) =@_; |
| 131 | +$block =$non_term_id .$line; |
| 132 | +$block =~s///g; |
| 133 | +$s ="\\|",$block =~s/$s//g; |
| 134 | +return$block; |
| 135 | +} |
| 136 | + |