ALDT (Arnulf's LaTeX Documentation Tool)

Check-in [1f9b6e521b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:initial version
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:1f9b6e521b0d2ab20149f20e2e90fbadefa0f1cf
User & Date: arnulf 2012-08-25 21:18:26
Context
2012-08-26
09:54
fixes and new code check-in: ca968628f6 user: arnulf tags: trunk
2012-08-25
21:18
initial version check-in: 1f9b6e521b user: arnulf tags: trunk
20:52
fixes check-in: 003edb3da8 user: arnulf tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added ScriptObjType.tcl.









































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#=====================================================
# ScriptObjType.tcl 
# *
# * ALDT (Arnulf's Latex Documentation Tool)
# * A Tcl parser for LaTeX files for generating document output (for example html)
# *
# * Initial parsing of a script
# *
# * Released under same BSD license as Tcl.
# * (Tcl BSD license found at <http://www.tcl.tk/software/tcltk/license.html>)
# *
# * Copyright 2012 Arnulf P. Wiedemann
#=====================================================

namespace eval ::Parser {

::itcl::class ScriptObjType {
  public common oid 0

  private variable id
  private variable debug
  private variable subst_debug

  constructor {} {}
  public method mySelf {}
  public method toString {}
  public method toDebugString {}
  public method setFromAny {obj_ptr flags}
  public method freeInternalRep {obj_ptr}
  public method dupInternalRep {src_ptr dup_ptr}
  public method makeScriptObj {parse_token text}
  public method getScript {obj_ptr}
  public method substSetFromAny {obj_ptr flags}
  public method getSubst {obj_ptr flags}
  public method substObj {subst_obj_ptr res_obj_ptr_ptr flags}
}

  # ==================== ScriptObjType ================================== 
::itcl::body ScriptObjType::constructor {} {
    incr oid
    set id $oid
    set debug 0
    set subst_debug 0
}

  # ==================== mySelf ================================== 
::itcl::body ScriptObjType::mySelf {} {
    String str = "ScriptObjType!"+id+"!";
    return str;
  }

  # ==================== toString =====================================
::itcl::body ScriptObjType::toString {} {
    return mySelf()+"!";
  }

  # ==================== toDebugString =====================================
::itcl::body ScriptObjType::toDebugString {} {
    StringBuffer str = new StringBuffer(mySelf()+"\n");
    return str.toString();
  }

  # ==================== setFromAny ==================================
::itcl::body ScriptObjType::setFromAny {obj_ptr flags} {
    Script script = new Script(interp);
    int line = 1;
    Parse parser = new Parse(interp);

//print("script sFA1!");
    script.text = interp.string_obj_type.getString(obj_ptr);
    script.text_len = interp.string_obj_type.getStringLength(obj_ptr);

//print("sFA1a!"+script.text+"!");
    /* Try to get information about filename / line number */
    if (obj_ptr.obj_type != 0 && obj_ptr.obj_type == OBJ_TYPE_SOURCE) {
      line = obj_ptr.sourceValue_GetLineNumber();
    }

    /* Initially parse the script into tokens (in tokenlist) */
    script.scriptTokenListInit();

//print("script sFA2!");
    parser.parserInit(script.text, script.text_len, line);
    while (! parser.parse_info.eof) {
      int ret = parser.parseScript();
//print("parseScript!"+getTokenString(parser.parse_info.token)+"!"+parser.getText()+"!");
      // FIXME should check ret code here !!!
      if (ret != OK) {
      }
//print("txt!"+parser.getText()+"!");
      script.scriptAddParseToken(parser.parse_info.start, parser.parse_info.end - parser.parse_info.start + 1, parser.parse_info.token, parser.parse_info.line, parser.parse_info.text);
    }
//print("script sFA3!");
    if (parser.parse_result.missing != ' ') {
      script.scriptTokenListFree();
      return ERROR;
    }

    /* Add a final EOF token */
    script.scriptAddParseToken(script.text_len, 0, TOKEN_EOF, 0, script.text);

//print("script sFA4!");
    /* Create the "real" script tokens from the initial token list */
    script.script_object.ref_count = 1;
    script.script_object.line = line;
    if (obj_ptr.obj_type != 0 && obj_ptr.obj_type == OBJ_TYPE_SOURCE) {
      script.script_object.file_name_obj = obj_ptr.sourceValue_GetFileNameObj();
    } else {
      script.script_object.file_name_obj = interp.empty_string_obj;
    }
    script.script_object.file_name_obj.incrRefCount("I_SCRIPT_OBJ_TYPE_1");
//print("script sFA5!");
    script.scriptObjAddTokens();
    /* No longer need the token list */
//print("script sFA6!");
    script.scriptTokenListFree();
    /* Free the old internal rep and set the new one. */
    obj_ptr.freeIntRep();
if (debug != 0) {
    print("SetFromAny!obj_type!"+getObjTypeString(obj_ptr.obj_type)+"!");
}
    obj_ptr.obj_type = OBJ_TYPE_SCRIPT;
//    obj_ptr.setIntRepPtr((Object)script);
    obj_ptr.scriptValue_SetScript(script);
    return OK;
  }

  # ==================== freeInternalRep =====================================
::itcl::body ScriptObjType::freeInternalRep {obj_ptr} {
    print("script freeInternalRep not yet implemented");
  }

  # ==================== duptInternalRep =====================================
::itcl::body ScriptObjType::dupInternalRep {src_ptr dup_ptr} {
    print("script dupInternalRep not yet implemented");
  }

  # ==================== makeScriptObj ==================================
::itcl::body ScriptObjType::makeScriptObj {parse_token text} {
    ApwtclObj obj_ptr;
    int idx = text.substring(parse_token.start, parse_token.start + parse_token.len).indexOf("\\");
    boolean have_backslash = idx >= 0 ? true : false;

//print("HAVEBSL!"+have_backslash+"!"+text.substring(parse_token.start, parse_token.start + parse_token.len)+"!");
    if ((parse_token.token == TOKEN_ESC || parse_token.token == TOKEN_QUOTE_ESC) && have_backslash) {
      /* Convert the backlash escapes . */
//print("HAVEBSL2!"+have_backslash+"!"+text.substring(parse_token.start, parse_token.start + parse_token.len)+"!");
      int len = parse_token.len;
      ArrayList<String> result_str = new ArrayList<String>();
      String str = text.substring(parse_token.start, parse_token.start + parse_token.len);
      len = interp.default_obj.escapeBackslash(result_str, str, len);
      str = result_str.get(0);
      obj_ptr = interp.string_obj_type.newStringObjNoAlloc(str, len, "SCRIPT_OBJ_TYPE_1");
    } else {
      /* REVISIT: Strictly, TOKEN_STR should replace <backslash><newline><whitespace>
       *         with a single space. This is currently not done.
       */
      obj_ptr = interp.string_obj_type.newStringObj(text.substring(parse_token.start, parse_token.start + parse_token.len), parse_token.len, "SCRIPT_OBJ_TYPE_2");

    }
    return obj_ptr;
  }

  # ==================== getScript ================================== 
::itcl::body ScriptObjType::getScript {obj_ptr} {
    int script_flags = 0;
    if (obj_ptr.obj_type == OBJ_TYPE_SCRIPT) {
      Script script = obj_ptr.scriptValue_GetScript();
      script_flags = script.script_object.subst_flags;
    }
    if ((obj_ptr.obj_type == 0) || (obj_ptr.obj_type != OBJ_TYPE_SCRIPT) || (script_flags != 0)) {
      if (setFromAny(obj_ptr, 0) != OK) {
        return null;
      }
    }
    return obj_ptr.scriptValue_GetScript();
  }

  # ==================== substSetFromAny ==================================
  # The subst object type reuses most of the data structures and functions
  # of the script object. Script's data structures are a bit more complex
  # for what is needed for [subst]itution tasks, but the reuse helps to
  # deal with a single data structure at the cost of some more memory
  # usage for substitutions. 
  #
  # This method takes the string representation of an object
  # as a Tcl string where to perform [subst]itution, and generates
  # the pre-parsed internal representation. 
::itcl::body ScriptObjType::substSetFromAny {obj_ptr flags} {
    Script script = new Script(interp);
    int line = 1;
    Parse parser = new Parse(interp);
    script.text = obj_ptr.getString();
    script.text_len = obj_ptr.getStringLength();

    /* Initially parse the script into tokens (in tokenlist) */
    script.scriptTokenListInit();
//print("substSetFromAny!"+obj_ptr+"!");
    parser.parserInit(script.text, script.text_len, line);
    boolean had_tokens = false;
    while (true) {
      parser.parseSubst(flags);
      if (parser.parse_info.eof) {
        /* Note that subst doesn't need the EOL token */
    	if (script.text_len == 1 && !had_tokens) {
          /* special case of only one character to parse */
    	  script.scriptAddParseToken(parser.parse_info.start, parser.parse_info.end - parser.parse_info.start + 1, TOKEN_ESC, parser.parse_info.line, parser.parse_info.text);
    	} else {
    	  if (!had_tokens && script.text_len > 1) {
        	  script.scriptAddParseToken(parser.parse_info.start, parser.parse_info.end - parser.parse_info.start + 1, parser.parse_info.token, parser.parse_info.line, parser.parse_info.text);    		  
    	  }
    	}
        break;
      }
if (subst_debug > 0) {
print("sSFA!"+Token.getTokenString(parser.parse_info.token)+"!"+parser.getText()+"!");
}
      script.scriptAddParseToken(parser.parse_info.start, parser.parse_info.end - parser.parse_info.start + 1, parser.parse_info.token, parser.parse_info.line, parser.parse_info.text);
      had_tokens = true;
    }


    /* Create the "real" subst/script tokens from the initial token list */
    script.script_object.ref_count = 1;
    script.script_object.subst_flags = flags;
    script.script_object.file_name_obj = interp.empty_string_obj;
    script.script_object.file_name_obj.incrRefCount("I_SCRIPT_OBJ_TYPE_2");
    script.substObjAddTokens(0);

    /* No longer need the token list */
    script.scriptTokenListFree();

    /* Free the old internal rep and set the new one. */
    obj_ptr.freeIntRep();
    obj_ptr.setIntRepPtr(script);
    obj_ptr.obj_type = OBJ_TYPE_SCRIPT;
    return OK;
  }

  # ==================== getSubst ==================================
::itcl::body ScriptObjType::getSubst {obj_ptr flags} {
    int script_flags = 0;
    if (obj_ptr.obj_type == OBJ_TYPE_SCRIPT) {
      Script script = obj_ptr.scriptValue_GetScript();
      script_flags = script.script_object.subst_flags;
    }
    if ((obj_ptr.obj_type != OBJ_TYPE_SCRIPT) || (script_flags != flags)) {
      substSetFromAny(obj_ptr, flags);
    }
    return (Script)obj_ptr.getIntRepPtr();
}

  # ==================== substObj ================================== */
  # Performs commands,variables,blackslashes substitution,
  # storing the result object (with refcount 0) into
  #res_obj_ptr_ptr. 

::itcl::body ScriptObjType::substObj {subst_obj_ptr res_obj_ptr_ptr flags} {
    Script script = getSubst(subst_obj_ptr, flags);
    
    subst_obj_ptr.incrRefCount("I_SCRIPT_OBJ_TYPE_3");      /* Make sure it's shared. */
    /* In order to preserve the internal rep, we increment the
     * ref_count field of the script internal rep structure. 
     */
    script.script_object.ref_count++;
//print("substObj!"+script.script_object.len+"!"+script.toDebugString()+"!");
    res_obj_ptr_ptr.add(interp.eval_statement.interpolateTokens(script, 0, script.script_object.len, flags));
    script.script_object.ref_count--;
    subst_obj_ptr.decrRefCount("D_SCRIPT_OBJ_TYPE_1");
    if (res_obj_ptr_ptr.get(0) == null) {
      return false
    }
    return true
  }

}