xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
<root> / docs / zograscope.md (213ea431a1d41b30e1dbe6b5bcb6d91cb1312086) (8,171B) (mode 100644) [raw]
NAME
====

*zograscope* is a collection of source-processing tools spawned out of
syntax-aware diffing.

The nature of syntax-aware diff requires knowledge of structure of the code,
which can be used to build other simple tools that can benefit from this
information.  Competing with real language front-ends in the level of accuracy
is not possible, but making some things that are one step further than regular
text-processing utilities seems feasible and the result might be even more
practical than some of the more elaborate tools which end up requiring
complicated setup process.

LANGUAGE SUPPORT
================

| Language  | Status
|-----------|-------------------------------------------------------------------
|  C        | C11 and earlier with common extensions, but without K&R syntax
|  C++      | C++14 and earlier with common extensions
|  Bash     | Not targeting a specific version
|  GNU Make | Most of the syntax
|  Lua      | Version 5.4

C
-

The exact grammar is that of C11 with extensions implemented in popular
compilers and additional extensions needed to allow processing of code with
macros.

Note the following:

 * old K&R style of function declarations isn't parsed (there might be a
   workaround for it, but this syntax is deprecated either way)
 * preprocessor directives aren't tokenized according to language rules yet,
   but their contents is diffed
 * extensive use of macros in unusual places might not be parsed (this probably
   won't change)

Other than that code in C89, C99, C11 and GNU-versions of C language should be
recognized.

C++
---

C++ support relies on external application called [srcml][srcml] and requires it
to be installed in binary form (not necessary during build).

Reported standard version supported by `srcml` is C++14, so all previous ones
should work too.  Although their parser doesn't handle all language constructs
equally well, it's seems to be good enough, especially for a ready to use parser
that wasn't that hard to integrate.

Note the following:

 * the tuning of comparison is in progress and will be refined over time

Bash
----

The source of the grammar doesn't explicitly specify the version.

GNU Make
--------

It's hard to measure level of support in case of GNU Make, because there seem to
be no reference for the syntax itself apart from documentation, which is not
concise.

Yet parser is capable of processing quite complicated examples of Makefiles
(like the one used in this project or generated by `automake`) which contain
many features most people don't know exist.  It's definitely not 100%, but 90%
or even more of all existing Makefiles out there should be parsed.

Note the following:

 * the comparison might not produce best results on Makefiles as it needs
   some tuning, this should happen over time (Makefiles aren't changed that
   often)

Lua
---

Note the following:

 * non-5.4 versions should work, but can produce worse results (however, syntax
   is normally backward compatible so this shouldn't happen often)

Other
-----

More languages should be added in the future, maybe with external parsers that
are capable of preserving all information about the source code.

CONFIGURATION
=============

Configuration is done per directory tree ("root") which is the closes parent (or
current directory) that contains `.zs/` directory.  The `.zs/` directory
contains files which define how contents of the root is to be processed.
Settings from multiple nested roots are not combined.

`.zs/exclude` file
------------------

A `.gitignore`-like (or `.git/info/exclude`-like) file that lists paths relative
to the root.  The purpose is to exclude uninteresting files (automatically
generated, third-party or otherwise).  `.zs/exclude` is used by tools that
search for files automatically and doesn't prevent the use of the same files
when they are specified explicitly.

The following kinds of entries are recognized:

* empty lines, which are ignored
* lines that start with a `#` (comments), which are ignored
* lines that end with `/` match only directories, the `/` is stripped and line
  processing continues
* lines without `/` are treated as shell-like globs against filename which apply
  at any directory level and define paths whose processing should be skipped
* lines that start with `!` define exception from rules that precede them, you
  can't undo exclusion of files in excluded directories, for the purpose of this
  discussion the `!` is stripped and line processing continues
* lines that start with `/` always match paths instead of filename and provide a
  way to specify files to be ignored only in the root, otherwise they are
  processed as specified in the next item
* other lines are treated as shell-like globs against paths relative to the
  root (leading `/` is allowed, but has no effect other than changing type of a
  match) which define paths whose processing should be skipped

No way to escape leading `#` and `!` or a newline at the moment.

Globs support the following: `[{char-class}]`, `[!{char-class}]`,
`[^{char-class}]`, `?` (doesn't match `/`), `*` (matches any (including zero)
number of characters except for `/`) and `\{char}` (matches literal `{char}`).

Example:

```gitignore
# .zs/exclude

# automatically generated sources
src/c/c11-lexer.gen.cpp
src/c/c11-parser.gen.cpp
src/make/*.gen.*

# Qt-produced sources
ui_*.h
moc_*.cpp
moc_*.h

# file in root
/config.h
```

`.zs/attributes` file
---------------------

Borrowing from the `git` project here again.  This file consists of lines
matching paths to attributes.  Lines are trimmed before being processed.

Empty lines and comments work like in `.zs/excludes` file, all other lines
follow this pattern:

    exclude-expr [attr1=value1 [attr2=value2 [...]]]

Expressions that define exceptions (start with `!`) are recognized but ignored
to keep syntax consistent between different files, which basically makes them
another type of comments.

Each line of the file is visited in top down order and attributes from every
matching entry are merged with the current state.  Hierarchy of configuration
values:

 1. Default values (lowest priority)
 2. Attributes
 3. Command-line parameters (highest priority)

Supported attributes:

* `lang`\
  Default: ""\
  Those accepted by `--lang` command-line option: bash, c, cxx, make, lua
* `tab-size`\
  Default: 4\
  Value should be an integer that's greater than zero

Unknown attributes are ignored.

Example:

```
# .zs/attributes

*.c tab-size=8
*.h tab-size=8 lang=c
tab-2.[ch] tab-size=2

# any.c has tab-size=8
# tab-2.c has tab-size=2
# tab-2.h has tab-size=2 lang=c
# any.h has tab-size=8 lang=c
# any.cpp has tab-size=4
```

INVOCATION
==========

All tools have common and specific command-line arguments.  Tool-specific
arguments are described on the page of the tool, common ones are below.

`-h`, `--help` \
print help message

`--dry-run` \
parse and exit

`--debug[=g]` \
enable debugging of grammar (`g` flag) and/or stree (`s` flag)

`--dump[=t]` \
display tree (`t` flag) and/or stree (`s` flag)

`--time-report` \
report time spent on different activities

`--color` \
force colorization of output

`--lang` _name_ \
force specific language (`bash`, `c`, `cxx`, `make`, `lua`) and
disable auto-detection

`--no-pager` \
never spawn a pager for output

BEHAVIOUR
=========

Pager
-----

By default when output is a terminal (not the case when invoked by Git), `less`
is spawn if output is about the size of the screen.  Use `--no-pager` to change
the behaviour.

Language detection
------------------

By default language is auto-detected based on file name.  When name is not
recognized the file is parsed as C.

TOOLS
=====

[**zs-diff**(1)](./zs-diff.md)

A terminal-based syntax-aware diff.

[**zs-find**(1)](./zs-find.md)

Grep-like tool that finds elements of source code structure.

[**zs-gdiff**(1)](./zs-gdiff.md)

A Qt5 GUI version of syntax-aware diff.

[**zs-hi**(1)](./zs-hi.md)

Simple syntax highlighter for xterm-256color palette.

[**zs-stats**(1)](./zs-stats.md)

Counter of lines of code.

[**zs-tui**(1)](./zs-tui.md)

TUI interface with underdefined scope of functionality.
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/zograscope

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/zograscope

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master