reader_peg.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*- */
00002 /*  -- $Id: reader_peg.tcl,v 1.1 2005/09/28 04:51:22 andreas_kupries Exp $ ---*/
00003 /* */
00004 /*  PAGE plugin - reader - PEG ~ Parsing Expression Grammar*/
00005 /* */
00006 
00007 /*  ### ### ### ######### ######### #########*/
00008 /*  Imported API*/
00009 
00010 /*  -----------------+--*/
00011 /*  page_read        | Access to the input stream.*/
00012 /*  page_read_done   |*/
00013 /*  page_eof         |*/
00014 /*  -----------------+--*/
00015 /*  page_info        | Reporting to the user.*/
00016 /*  page_warning     |*/
00017 /*  page_error       |*/
00018 /*  -----------------+--*/
00019 /*  page_log_error   | Reporting of internals.*/
00020 /*  page_log_warning |*/
00021 /*  page_log_info    |*/
00022 /*  -----------------+--*/
00023 
00024 /*  ### ### ### ######### ######### #########*/
00025 /*  Exported API*/
00026 
00027 /*  -----------------+--*/
00028 /*  page_rfeature    | Query for special plugin features page might wish to use.*/
00029 /*  page_rtime       | Activate collection of timing statistics.*/
00030 /*  page_rgettime    | Return the collected timing statistics.*/
00031 /*  page_rlabel      | User readable label for the plugin.*/
00032 /*  page_rhelp       | Doctools help text for plugin.*/
00033 /*  page_roptions    | Options understood by plugin.*/
00034 /*  page_rconfigure  | Option (re)configuration.*/
00035 /*  page_rdata       | External access to processed input stream.*/
00036 /*  page_rrun        | Process input stream per plugin configuration and hardwiring.*/
00037 /*  -----------------+--*/
00038 
00039 /*  ### ### ### ######### ######### #########*/
00040 /*  Requisites*/
00041 
00042 package require page::util::norm::peg ; /*  Normalize AST generated by reader of PEG grammars*/
00043 package require page::parse::peg      ; /*  Mengine based parser for PE grammars.*/
00044 package require struct::tree          ; /*  Data structure.*/
00045 package require grammar::me::util     ; /*  AST conversion*/
00046 
00047 global usec
00048 global timed
00049     timed =  0
00050 
00051 global cline
00052 global ccol
00053 
00054 /*  ### ### ### ######### ######### #########*/
00055 /*  Implementation of exported API*/
00056 
00057 ret  page_rlabel () {
00058     return {Parsing Expression Grammar}
00059 }
00060 
00061 ret  page_rfeature (type key) {
00062     return [string eq $key timeable]
00063 }
00064 
00065 ret  page_rtime () {
00066     global timed
00067     set    timed 1
00068     return
00069 }
00070 
00071 ret  page_rgettime () {
00072     global  usec
00073     return $usec
00074 }
00075 
00076 ret  page_rhelp () {
00077     return {}
00078 }
00079 
00080 ret  page_roptions () {
00081     return {}
00082 }
00083 
00084 ret  page_rconfigure (type option , type value) {
00085     return -code error "Cannot set value of unknown option \"$option\""
00086 }
00087 
00088 /*  proc page_rdata {} {}*/
00089 /*  Created in 'Initialize'*/
00090 
00091 ret  page_rrun () {
00092     global timed usec cline ccol
00093     page_log_info "reader/peg/run/parse"
00094 
00095     set ast {}
00096     set err {}
00097 
00098     # Location of the next character to be read.
00099     set cline 1
00100     set ccol  0
00101 
00102     if {$timed} {
00103     set usec [lindex [time {
00104         set ok [::page::parse::peg::parse ::Next err ast]
00105     }] 0] ; #{}
00106     } else {
00107     set ok [::page::parse::peg::parse ::Next err ast]
00108     }
00109     page_read_done
00110     page_log_info "reader/peg/run/check-for-errors"
00111 
00112     if {!$ok} {
00113     foreach {olc   messages} $err     break
00114     foreach {offset linecol} $olc     break
00115     foreach {line       col} $linecol break
00116 
00117     set olc [string map {{ } _} \
00118         [format %5d $line]]@[string map {{ } _} \
00119         [format %3d $col]]/([format %5d $offset])
00120 
00121     foreach m $messages {
00122         page_log_error "reader/peg/run: $olc: $m"
00123         page_error $m $linecol
00124     }
00125 
00126     page_log_info "reader/peg/run/failed"
00127     return {}
00128     }
00129 
00130     page_log_info "reader/peg/run/ast-conversion"
00131 
00132     struct::tree                        ::tree
00133     ::grammar::me::util::ast2etree $ast ::grammar::me::tcl ::tree
00134     ::page::util::norm::peg             ::tree
00135 
00136     set ast [::tree serialize]
00137     ::tree destroy
00138 
00139     page_log_info "reader/peg/run/ok"
00140     return $ast
00141 }
00142 
00143 /*  ### ### ### ######### ######### #########*/
00144 /*  Internal helper code.*/
00145 
00146 ret  Next () {
00147     global cline ccol
00148 
00149     if {[page_eof]} {return {}}
00150 
00151     set ch [page_read 1]
00152 
00153     if {$ch eq ""} {return {}}
00154 
00155     set tok [list $ch {} $cline $ccol]
00156 
00157     if {$ch eq "\n"} {
00158     incr cline ; set ccol 0
00159     } else {
00160     incr ccol
00161     }
00162 
00163     return $tok
00164 }
00165 
00166 /*  ### ### ### ######### ######### #########*/
00167 /*  Initialization*/
00168 
00169 package provide page::reader::peg 0.1
00170 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1