reader_lemon.tcl

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

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1