A pretty decent macro processor

c095888 Fix Hare docs link

~smlavine pushed to ~smlavine/eaton git

4 months ago

1d3f350 Add Contributing and Copyright section to README.md

~smlavine pushed to ~smlavine/eaton git

4 months ago


A pretty decent macro processor in less than 300 lines of Hare.

This is still a work in progress, and there are some bugs and features still being worked on; in particular, nested macros.

#Example usage

$ make
hare build
$ cat input1.txt
Hi there, ``upper!(everyone)''!
$ ./eaton < input1.txt
Hi there, EVERYONE!


eaton is meant to vendored and extended, not installed like most other programs. Simply clone this repository where you wish, run make, and you're ready to go. If you are incorporating eaton into an existing repository, you may prefer to use git-subtree:

$ git subtree --squash -P eaton/ add https://git.sr.ht/~smlavine/eaton master

#Writing your own macros

To add new macros, simply add a new Hare source file to the macro module. Use the existing macro definitions as examples. Even complex macros shouldn't take more than ten or twenty lines of your own code.

All of the macros provided with eaton are named like name!(arguments). But this is just a convention; any macros you write yourself do not have to follow it.

#Regular expressions

Take care when writing the regular expressions used to match your macros. Writing a regular expression that always matches (such as (.*)) will crash the program. The Hare regex engine is greedy, so if matching for text within parens, don't match closing parens in your capture. Consider a simple, correct regular expression, as defined in macro/upper.ha:

def UPPER_RE = `upper!\(([^)]*)\)`;

The contents of the capture group is what will be provided to the macro for formatting. Instead of capturing .*, we capture [^)]*, preventing the expression from matching beyond what is intended.

For more information on regular expressions, consider reading the documentation for the Hare regex module or regex(7).

#Modifying state

There is no method of modifying state (i.e. defining macros or setting variables) from the input of a macro itself; this would increase implementation complexity considerably. However, the provided macros getenv! and system! are enough to interact with the environment and state can be stored and retrieved from it however you like.

Copyright (C) 2023 Sebastian LaVine <mail@smlavine.com>

Licensed under the terms of the GNU GPL, version 3 or later.

Patches? Feedback? Send them to ~smlavine/public-inbox@lists.sr.ht.