Skip to content

Commit 3b9df16

Browse files
committed
docs(web): update optscript.rst (in progress)
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 9e8a785 commit 3b9df16

File tree

1 file changed

+138
-38
lines changed

1 file changed

+138
-38
lines changed

docs/optscript.rst

+138-38
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,29 @@ operators of Optscript and PostScript are the same. You can get the
1919
basic knowledge for using Optscript from the materials for learning
2020
PostScript.
2121

22-
"PostScript Language Tutorial & Cookbook" published by Adobe Systems
23-
Inc. The book is known as "blue book". This is the best place to
22+
"PostScript Language Tutorial & Cookbook" is a book published by Adobe
23+
Systems Inc. The book, known as "blue book", is the best place to
2424
start. PostScript is a language for controlling printers. So it has
25-
many graphical operators. Optscript is for tagging, and doesn't have
26-
such graphical operators. So you can skip the sections about graphics
25+
many graphical operators. Optscript doesn't have such graphical
26+
operators. So you can skip the sections about graphics
2727
(but you may want to read them because the book is written well).
2828

2929
Ghostscript (``gs`` or ``gsnd``) is an interpreter for the PostScript
3030
language and PDF files. Unlike Optscript, it implements the full-set of
3131
PostScript features including graphical operators. It is available
32-
under either the GNU GPL Affero license. You can Ghostscript while
32+
under either the GNU GPL Affero license. You can use Ghostscript while
3333
reading the blue book. Do web searching to know about Ghostscript.
3434

35-
``optscript`` is an command that source files are included in
36-
Universal Ctags source tree. You can use it as the replacement of
37-
``gs``. However, I recommend you to have ``gs`` at hand because
38-
``optscript`` may have bugs. ``gs`` is much mature than ``optscript``.
39-
Having two interpreters helps you to know correct behavior.
35+
``optscript`` is a command that source files are included in the
36+
source tree of Universal Ctags. You can use it as the replacement of
37+
``gs`` to learn PostScript. However, I recommend you to have ``gs`` at
38+
hand because ``optscript`` may have bugs. ``gs`` is much mature than
39+
``optscript``. Having two interpreters helps you to know expected
40+
behavior of the language.
4041

4142
Though ``gs`` has much higher qualities than ``optscript``, eventually
4243
you may have to build the ``optscript`` command to learn Optscript
43-
specific operators. You can built the command with "``make
44+
specific operators for tagging. You can built the command with "``make
4445
optscript``".
4546

4647
* red book
@@ -164,9 +165,29 @@ easily, Python sessions doing the same as Optscript are also written.
164165
>>> add5_and_print(4)
165166
9
166167

167-
* string manipulation
168+
* String manipulation
169+
170+
- Comparison
171+
172+
Optscript:
173+
174+
.. code-block:: console
175+
176+
OPT> (abc) (efg) eq { (same) = } { (different) = } ifelse
177+
different
178+
OPT> (abc) (abc) eq { (same) = } { (different) = } ifelse
179+
same
180+
181+
Python:
182+
183+
.. code-block:: console
184+
185+
>>> if 'abc' == 'efg':
186+
... print ('same')
187+
... else:
188+
... print ('different')
189+
different
168190
169-
TBW
170191
171192
* array manipulation
172193

@@ -190,6 +211,8 @@ easily, Python sessions doing the same as Optscript are also written.
190211

191212
Optscript in ctags
192213
~~~~~~~~~~~~~~~~~~
214+
Optscript executable in ctags assumes that all tags are in the corkQueue and
215+
they have corkIndexes. See ":ref:`output-tag-stream`" about corkQueue and corkIndexes.
193216

194217
Related options
195218
...............
@@ -242,12 +265,12 @@ TBW: two timings of evaluation
242265

243266
Put code fragments at the end of options with surrounding "``{{``" and
244267
"``}}``". Though it is not impossible, a command line is not suitable
245-
place to put code fragments because the code fragments may be long.
246-
Instead, you should write them to a .ctags file.
268+
place to write code fragments because the options with code fragments
269+
may be too long. Instead, you should write them to your .ctags file.
247270

248-
.. warning:: An important rule in writing Optscript code in a file is
249-
the start marker, ``{{`` must be at the end of line, and the end
250-
marker ``}}`` must be at the beginning of line. If you break the
271+
.. warning:: An important rule in writing Optscript code in a .ctags
272+
file is the start marker, ``{{``, must be at the end of line, and the
273+
end marker, ``}}``, must be at the beginning of line. If you break the
251274
rule, the optlib loader of ctags fails to read your file.
252275

253276
``--_prelude-<LANG>`` is for specified code fragments run at the
@@ -269,62 +292,139 @@ represents the field has an operator for reading
269292
(``:fieldname``). ``w`` represents the field has an operator for
270293
writing (``fieldname:``).
271294

295+
``optlib2c``, a translator from .ctags file to C language source file
296+
supports the code framents. Some of optlib parsers integrated to
297+
Universal Ctags already use Optscript. You can find practical
298+
examples of Optscript in files in ``optlib`` directory. The positional
299+
rules about `{{` and `}}` is applicable to ``optlib2c``.
300+
301+
Data types
302+
..........
303+
304+
Non-standard data types for tagging are added. You can use it only in
305+
``ctags`` command, not in ``optscript`` command.
306+
307+
``matchloc``
308+
309+
This opaque data type is for representing a position in a source
310+
file. The name is an acronym for "match location". ``_matchloc``
311+
pushes a ``matchloc`` object to ``ostack``.
312+
313+
``corkIndex:int`` or ``index:int``
314+
315+
This represents a corkIndex. ``.`` pushes the corkIndex for a tag
316+
just created with the option owning the code frament.
317+
318+
``tag``
319+
320+
This represents a tag data structure which is not in the corkQueue
321+
yet. ``_tag`` is an operator pushing a ``tag`` object to
322+
``ostack``. ``_commit`` puts the ``tag`` object on ``ostack`` to
323+
the corkQueue, and pushes an integer as a corkIndex.
324+
272325
Operators
273326
............................
274327

275328
**.** -> ``-`` **.** ``corkIndex:int``
276329

277-
Push the cork index for the tag
330+
Push the cork index for the tag
278331

279332
**\\n** -> ``-`` **\\n** ``matchedString:string``
280333

281-
``n`` is an integer (0...9) representing a group in a pattern.
334+
``n`` is an integer (1...9) representing a group in a pattern.
282335
Push the matched string for the group.
283336

284337
``_matchloc``
285338

286-
TBW
339+
TBW
287340

288341
``:field`` (See the output of ``--_list-operators``)
289342

290-
Get the value for the specified field from a tag
343+
Get the value for the specified field from a tag
291344
and put it.
292345

293346
``field:`` (See the output of ``--_list-operators``)
294347

295-
Set a value at the stack to the specified field of a tag.
348+
Set a value at the stack to the specified field of a tag.
296349

297350
``_tag``
298351

299-
TBW
352+
TBW
300353

301-
``_COMMIT``
354+
``_commit``
302355

303-
TBW
356+
TBW
304357

305358
``_traced``
306359

307-
TBW
360+
TBW
308361

309-
Data types
310-
..........
311362

312-
``MATCHLOC``
363+
Recipes
364+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
313365

314-
TBW
366+
Arrange the name of a tag
367+
...........................................
315368

316-
``index:int``
369+
"input.foo":
317370

318-
TBW
371+
.. code-block::
319372
320-
``TAG``
373+
def a
374+
def b
321375
322-
TBW
376+
Goal: If a language specific extra ``extendedName`` is given, the
377+
parser for input.foo emits extra tags having ``X`` as prefix.
323378

324-
Recipes
325-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
379+
The base version of .ctags ("foo0.ctags"):
380+
381+
.. code-block:: ctags
382+
383+
--langdef=foo
384+
--map-foo=.foo
385+
--kinddef-foo=d,definition,definitions
386+
--regex-foo=/^def[ \t]+([a-z]+)/\1/d/
387+
388+
The tags output ("output0.tags") for the base version
389+
with "``--optoins=foo0.ctags input.foo``":
390+
391+
.. code-block:: tags
392+
393+
a input.foo /^def a$/;" d
394+
b input.foo /^def b$/;" d
395+
396+
The Optscript version of .ctags ("foo1.ctags") for achieving the goal:
397+
398+
.. code-block:: ctags
399+
400+
--langdef=foo
401+
--map-foo=.foo
402+
--kinddef-foo=d,definition,definitions
403+
--_extradef-foo=extendedName, tags prefixed with X
404+
--regex-foo=/^def[ \t]+([a-z]+)/\1/d/{{
405+
/foo.extendedName _extraenabled {
406+
mark \1 ?X _buildstring % name, \1 + 'X'
407+
/definition % kind
408+
1 _matchloc % location for \1
409+
_tag % a tag object is pushed.
410+
% ostack => tag
411+
dup
412+
% ostack => tag tag
413+
/foo.extendedName _markextra
414+
% ostack => tag
415+
_commit
416+
} if
417+
}}
418+
419+
The tags output ("output1.tags") for the Optscript version
420+
with "``--optoins=foo1.ctags --extras-foo=+'{extendedName}' input.foo``":
421+
422+
.. code-block:: tags
326423
327-
TBW
424+
a input.foo /^def a$/;" d
425+
aX input.foo /^def a$/;" d
426+
b input.foo /^def b$/;" d
427+
bX input.foo /^def b$/;" d
328428
329429
Difference between Optscript and PostScript
330430
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)