OpenResty Fanlang™ User Manual
fanlang - A Perl 6 dialect for implementing other DSLs
Table of Contents
Usage
$ cat a.fan
my $who = "world";
say "hello, $who!"
$ fan a.fan
hello, world!
Description
Fanlang is a optimizing compiler that converts a dialect of Perl 6 into standalone Lua code targeting OpenResty.
The motivation behind the fanlang compiler are:
We want to be much more productive when writing OpenResty-based software that is not performance sensitive, like a DSL compiler. Perl 6 (or even Perl 5) is much more expressive and much more powerful than Lua, especially for text processing tasks and implementing transcompilers. But Lua has its own strength in performance and simplicity. So we write fanlang that compiles Perl 6 into Lua for OpenResty.
We want the compiling speed or compiler performance to be (much) faster than the counterparts implemented in Perl 5.
Perl 6 features that will never be implemented
multi
methods andmulti
subs.- Perl 6 regexes (we use Perl 5 regexes and our own fanlang rule syntax).
Language features incompatible to Perl 6
Seq
Fanlang does not support the builtin Perl 6 class Seq
yet. In cases where Rakudo returns a Seq
object, fanlang
produces a List
object instead.
Regexes
Fanlang uses Perl 5 compatible regexes by default in its regexes. This means that the Perl 6 regex adverb :P5
is always in effect in fanlang code. Also, whitespace characters are not significant t in fanlang’s
Perl 5 compatible regexes, unless the regex adverb :s
is specified.
Grammars
Fanlang uses its own grammar rule syntax inspired by Parse::RecDescent and Pegex.
The “actions” class fed into Grammar objects’ parse()
method can inherit from the builtin class Actions
, which
provide useful methods like get-line()
for fetching the current line number in the input being parsed.
When the FANLANG_DEBUG=1
system environment variable is specified, the fanlang grammar engine will generate
tracing output for debugging parsers written in fanlang.
Error Handling
When an error happens in the slurp()
builtin or a Grammar object’s parse()
method, the corresponding builtin
fanlang routine will not throw an exception like Perl 6, instead, it returns Nil and sets the special variable
$!
to the error string.
Gist
The .gist()
output for some primitive values, like Nil
, False
, and True
, are in all lower case, that is,
their gist outputs are nil
, false
, and true
, respectively.
@*ARGS
The global variable @*ARGS()
is not allowed to used in any fan source that is imported by use
statement.
Genuine builtins
test-regex
The test-regex(regex)
subroutine accepts a fanlang string and checks whether it is a valid Perl compatible regex.
Returns True
if yes; returns Nil
and sets $!
to the error string otherwise.
is-primitive
Accepts an arbitrary fanlang value as the sole argument. Returns True
when the value is a “primitive value”, e.g.,
a boolean, a number, a string, or just Nil
. Returns False
otherwise. Neither objects, types, arrays, nor hashes are
considered “primitive values”.
file-exists
Accepts a fanlang string as a file name. Returns True
when the file exists; False
otherwise.
class Attribute
bare_name
This read-only attribute returns the bare name of the current Attribute object, without the sigil and twigil parts.
Author
Yichun Zhang (agentzh) <yichun@openresty.com>
Copyright & Licenses
Copyright (C) 2016-2021, OpenResty Inc. All rights reserved.
This software is proprietary and must not be redistributed or shared at all.