1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
# -*- tcl -*-
# Copyright (c) 2009-2010 Andreas Kupries <andreas_kupries@sourceforge.net>
# Canned configuration for the converter to C/PARAM representation,
# causing generation of a proper critcl-based parser.
# The requirements of the embedded template are not our requirements.
# @mdgen NODEP: critcl
# # ## ### ##### ######## ############# #####################
## Requirements
package require Tcl 8.5 ; # Required runtime.
# # ## ### ##### ######## ############# #####################
##
namespace eval ::pt::cparam::configuration::critcl {
namespace export def
namespace ensemble create
# @mdgen OWNER: rde_critcl/util.*
# @mdgen OWNER: rde_critcl/stack.*
# @mdgen OWNER: rde_critcl/tc.*
# @mdgen OWNER: rde_critcl/param.*
# Access to the rde_critcl files forming the low-level runtime
variable selfdir [file dirname [file normalize [info script]]]
}
# # ## ### ##### ######## #############
## Public API
# Check that the proposed serialization of an abstract syntax tree is
# indeed such.
proc ::pt::cparam::configuration::critcl::def {class pkg cmd} {
# TODO :: See if we can consolidate the API for converters,
# TODO :: plugins, export manager, and container in some way.
# TODO :: Container may make exporter manager available through
# TODO :: public method.
# class = The namespace/prefix for the generated commands.
# pkg = The name of generated package.
if {[string first :: $class] < 0} {
set cheader $class
set ctrailer $class
} else {
set cheader [namespace qualifier $class]
set ctrailer [namespace tail $class]
}
{*}$cmd -main MAIN
{*}$cmd -indent 8
{*}$cmd -template [string trim \
[string map \
[list \
@@RUNTIME@@ [GetRuntime] \
@@PKG@@ $pkg \
@@CLASS@@ $class \
@@CHEAD@@ $cheader \
@@CTAIL@@ $ctrailer \
\n\t \n \
] {
## -*- tcl -*-
##
## Critcl-based C/PARAM implementation of the parsing
## expression grammar
##
## @name@
##
## Generated from file @file@
## for user @user@
##
# # ## ### ##### ######## ############# #####################
## Requirements
package require Tcl 8.4
package require critcl
# @sak notprovided @@PKG@@
package provide @@PKG@@ 1
# Note: The implementation of the PARAM virtual machine
# underlying the C/PARAM code used below is inlined
# into the generated parser, allowing for direct access
# and manipulation of the RDE state, instead of having
# to dispatch through the Tcl interpreter.
# # ## ### ##### ######## ############# #####################
##
namespace eval ::@@CHEAD@@ {
# # ## ### ##### ######## ############# #####################
## Supporting code for the main command.
catch {
#critcl::cheaders -g
#critcl::debug memory symbols
}
# # ## ### ###### ######## #############
## RDE runtime, inlined, and made static.
# This is the C code for the RDE, i.e. the implementation
# of pt::rde. Only the low-level engine is imported, the
# Tcl interface layer is ignored. This generated parser
# provides its own layer for that.
critcl::ccode {
/* -*- c -*- */
#include <string.h>
#define SCOPE static
@@RUNTIME@@
}
# # ## ### ###### ######## #############
## BEGIN of GENERATED CODE. DO NOT EDIT.
critcl::ccode {
/* -*- c -*- */
@code@
}
## END of GENERATED CODE. DO NOT EDIT.
# # ## ### ###### ######## #############
# # ## ### ###### ######## #############
## Global PARSER management, per interp
critcl::ccode {
/* -*- c -*- */
typedef struct PARSERg {
long int counter;
char buf [50];
} PARSERg;
static void
PARSERgRelease (ClientData cd, Tcl_Interp* interp)
{
ckfree((char*) cd);
}
static const char*
PARSERnewName (Tcl_Interp* interp)
{
#define KEY "tcllib/parser/@@PKG@@/critcl"
Tcl_InterpDeleteProc* proc = PARSERgRelease;
PARSERg* parserg;
parserg = Tcl_GetAssocData (interp, KEY, &proc);
if (parserg == NULL) {
parserg = (PARSERg*) ckalloc (sizeof (PARSERg));
parserg->counter = 0;
Tcl_SetAssocData (interp, KEY, proc,
(ClientData) parserg);
}
parserg->counter ++;
sprintf (parserg->buf, "@@CTAIL@@%d", parserg->counter);
return parserg->buf;
#undef KEY
}
static void
PARSERdeleteCmd (ClientData clientData)
{
/*
* Release the whole PARSER
* (Low-level engine only actually).
*/
rde_param_del ((RDE_PARAM) clientData);
}
}
# # ## ### ##### ######## #############
## Functions implementing the object methods, and helper.
critcl::ccode {
static int COMPLETE (RDE_PARAM p, Tcl_Interp* interp);
static int parser_PARSE (RDE_PARAM p, Tcl_Interp* interp, int objc, Tcl_Obj* CONST* objv)
{
int mode;
Tcl_Channel chan;
if (objc != 3) {
Tcl_WrongNumArgs (interp, 2, objv, "chan");
return TCL_ERROR;
}
chan = Tcl_GetChannel(interp,
Tcl_GetString (objv[2]),
&mode);
if (!chan) {
return TCL_ERROR;
}
rde_param_reset (p, chan);
MAIN (p) ; /* Entrypoint for the generated code. */
return COMPLETE (p, interp);
}
static int parser_PARSET (RDE_PARAM p, Tcl_Interp* interp, int objc, Tcl_Obj* CONST* objv)
{
char* buf;
int len;
if (objc != 3) {
Tcl_WrongNumArgs (interp, 2, objv, "text");
return TCL_ERROR;
}
buf = Tcl_GetStringFromObj (objv[2], &len);
rde_param_reset (p, NULL);
rde_param_data (p, buf, len);
MAIN (p) ; /* Entrypoint for the generated code. */
return COMPLETE (p, interp);
}
static int COMPLETE (RDE_PARAM p, Tcl_Interp* interp)
{
if (rde_param_query_st (p)) {
long int ac;
Tcl_Obj** av;
rde_param_query_ast (p, &ac, &av);
if (ac > 1) {
long int lsc;
long int* lsv;
Tcl_Obj** lv = NALLOC (3+ac, Tcl_Obj*);
rde_param_query_ls (p, &lsc, &lsv);
memcpy(lv + 3, av, ac * sizeof (Tcl_Obj*));
lv [0] = Tcl_NewObj ();
lv [1] = Tcl_NewIntObj (1 + lsv [lsc-1]);
lv [2] = Tcl_NewIntObj (rde_param_query_cl (p));
Tcl_SetObjResult (interp, Tcl_NewListObj (3, lv));
ckfree ((char*) lv);
} else {
Tcl_SetObjResult (interp, av [0]);
}
return TCL_OK;
} else {
Tcl_Obj* xv [1];
const ERROR_STATE* er = rde_param_query_er (p);
Tcl_Obj* res = rde_param_query_er_tcl (p, er);
xv [0] = Tcl_NewStringObj ("pt::rde",-1);
Tcl_ListObjReplace(interp, res, 0, 1, 1, xv);
Tcl_SetObjResult (interp, res);
return TCL_ERROR;
}
}
}
# # ## ### ##### ######## #############
## Object command, method dispatch.
critcl::ccode {
static int parser_objcmd (ClientData cd, Tcl_Interp* interp, int objc, Tcl_Obj* CONST* objv)
{
RDE_PARAM p = (RDE_PARAM) cd;
int m, res;
static CONST char* methods [] = {
"destroy", "parse", "parset", NULL
};
enum methods {
M_DESTROY, M_PARSE, M_PARSET
};
if (objc < 2) {
Tcl_WrongNumArgs (interp, objc, objv, "option ?arg arg ...?");
return TCL_ERROR;
} else if (Tcl_GetIndexFromObj (interp, objv [1], methods, "option",
0, &m) != TCL_OK) {
return TCL_ERROR;
}
/* Dispatch to methods. They check the #args in
* detail before performing the requested
* functionality
*/
switch (m) {
case M_DESTROY:
if (objc != 2) {
Tcl_WrongNumArgs (interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_DeleteCommandFromToken(interp, (Tcl_Command) rde_param_query_clientdata (p));
return TCL_OK;
case M_PARSE: res = parser_PARSE (p, interp, objc, objv); break;
case M_PARSET: res = parser_PARSET (p, interp, objc, objv); break;
default:
/* Not coming to this place */
ASSERT (0,"Reached unreachable location");
}
return res;
}
}
# # ## ### ##### ######## #############
# Class command, i.e. object construction.
critcl::ccommand @@CTAIL@@_critcl {dummy interp objc objv} {
/*
* Syntax: No arguments beyond the name
*/
RDE_PARAM parser;
CONST char* name;
Tcl_Obj* fqn;
Tcl_CmdInfo ci;
Tcl_Command c;
#define USAGE "?name?"
if ((objc != 2) && (objc != 1)) {
Tcl_WrongNumArgs (interp, 1, objv, USAGE);
return TCL_ERROR;
}
if (objc < 2) {
name = PARSERnewName (interp);
} else {
name = Tcl_GetString (objv [1]);
}
if (!Tcl_StringMatch (name, "::*")) {
/* Relative name. Prefix with current namespace */
Tcl_Eval (interp, "namespace current");
fqn = Tcl_GetObjResult (interp);
fqn = Tcl_DuplicateObj (fqn);
Tcl_IncrRefCount (fqn);
if (!Tcl_StringMatch (Tcl_GetString (fqn), "::")) {
Tcl_AppendToObj (fqn, "::", -1);
}
Tcl_AppendToObj (fqn, name, -1);
} else {
fqn = Tcl_NewStringObj (name, -1);
Tcl_IncrRefCount (fqn);
}
Tcl_ResetResult (interp);
if (Tcl_GetCommandInfo (interp,
Tcl_GetString (fqn),
&ci)) {
Tcl_Obj* err;
err = Tcl_NewObj ();
Tcl_AppendToObj (err, "command \"", -1);
Tcl_AppendObjToObj (err, fqn);
Tcl_AppendToObj (err, "\" already exists", -1);
Tcl_DecrRefCount (fqn);
Tcl_SetObjResult (interp, err);
return TCL_ERROR;
}
parser = rde_param_new (sizeof(p_string)/sizeof(char*), (char**) p_string);
c = Tcl_CreateObjCommand (interp, Tcl_GetString (fqn),
parser_objcmd, (ClientData) parser,
PARSERdeleteCmd);
rde_param_clientdata (parser, (ClientData) c);
Tcl_SetObjResult (interp, fqn);
Tcl_DecrRefCount (fqn);
return TCL_OK;
}
##
# # ## ### ##### ######## #############
}
# # ## ### ##### ######## ############# #####################
## Ready (Note: Our package provide is at the top).
return
}]]
return
}
proc ::pt::cparam::configuration::critcl::GetRuntime {} {
# This is the C code for the RDE, i.e. the implementation of
# pt::rde. Only the low-level engine is imported, the Tcl
# interface layer is ignored. This generated parser provides its
# own layer for that.
# We are inlining the code (making the functions static) to
# prevent any conflict with the support for pt::rde, should both
# be put into the same shared library.
variable selfdir
set code {}
foreach f {
rde_critcl/util.h
rde_critcl/stack.h
rde_critcl/tc.h
rde_critcl/param.h
rde_critcl/util.c
rde_critcl/stack.c
rde_critcl/tc.c
rde_critcl/param.c
} {
# Load C code.
set c [open $selfdir/$f]
set d [read $c]
close $c
# Strip include directives and anything explicitly excluded.
set skip 0
set n {}
foreach l [split $d \n] {
if {[string match {*#include*} $l]} {
continue
}
if {[string match {*SKIP START*} $l]} {
set skip 1
continue
}
if {[string match {*SKIP END*} $l]} {
set skip 0
continue
}
if {$skip} continue
lappend n $l
}
set d [join $n \n]
# Strip comments, trailing whitespace, empty lines.
set d [regsub -all {/\*.*?\*/} $d {}]
set d [regsub -all {//.*?\n} $d {}]
set d [regsub -all {[ ]+$} $d {}]
while {1} {
set n [string map [list \n\n \n] $d]
if {$n eq $d} break
set d $n
}
# Indent code.
lappend code "#line 1 \"$f\""
foreach l [split $d \n] {
if {$l ne ""} { set l \t$l }
lappend code $l
}
}
#lappend code "#line x \"X\""
return [join $code \n]
}
# # ## ### ##### ######## #############
namespace eval ::pt::cparam::configuration::critcl {}
# # ## ### ##### ######## ############# #####################
## Ready
package provide pt::cparam::configuration::critcl 1.0.1
return
|