Adding more language grammars

This commit is contained in:
Bruce Hill 2021-01-17 19:42:11 -08:00
parent 23f9b7ade9
commit 51313c4773
5 changed files with 91 additions and 0 deletions

14
grammars/README.md Normal file
View File

@ -0,0 +1,14 @@
# BP Grammars
The files in this directory are predefined grammars for different languages and
contexts. They are intended to be used for common search patterns, and **not**
intended to be complete PEG definitions of language grammars, other than
[bp.bp](./bp.bp), which is included for stress-testing purposes, as well as a
showcase of some BP features.
## Adding Grammars
If you want to add your own grammar, the easiest way to do so is to create a
`.bp` file in `~/.config/bp/`. The syntax for grammar files is fully and
formally defined in [bp.bp](./bp.bp), but in short, it's a list of
whitespace-separated rule definitions of the form `id __ ":" __ pattern`.

View File

@ -1,4 +1,9 @@
# This is a file defining the BP grammar using BP syntax
#
# This is a complete definition of the grammar of BP grammar files, but it's
# mainly intended to be used as a proof-of-concept, and a stress-test for BP.
# The grammar files provided with BP are not otherwise intended to be full
# language grammars.
Grammar: __ *(Def [__`;])%__ __ ($$ / @!=(..$$%\n => "Could not parse this code"))
Def: @name=id __ `: __ (

23
grammars/c.bp Normal file
View File

@ -0,0 +1,23 @@
# Syntax definition for some C-specific patterns
#
# NOTE: this is *NOT* intended to be a complete definition of the language's
# syntax! Providing a full language grammar is overkill, because the intended
# use case is finding/replacing string patterns. This task does not require a
# full parse tree, and having one makes the task considerably more complicated.
# See the accompanying README.md for more info.
comment: "//" .. $ / "/*" .. "*/" % \n
string: `"..`" % (`\.)
char: `' [`\] . `'
array: `{..`} % (array/comment/\n)
struct: array
keyword: |(
"auto" / "break" / "case" / "char" / "const" / "continue" / "default" / "do" /
"double" / "else" / "enum" / "extern" / "float" / "for" / "goto" / "if" /
"int" / "long" / "register" / "return" / "short" / "signed" / "sizeof" /
"static" / "struct" / "switch" / "typedef" / "union" / "unsigned" / "void" /
"volatile" / "while"
)|
function-def: ^_ 2+(keyword / id / `*) % __ parens (__`; / >(__`{))
function: function-def __ braces
macro: ^"#define " ..$ *(<`\ \n..$)

19
grammars/lua.bp Normal file
View File

@ -0,0 +1,19 @@
# Syntax definitions for some Lua-specific patterns
#
# NOTE: this is *NOT* intended to be a complete definition of the language's
# syntax! Providing a full language grammar is overkill, because the intended
# use case is finding/replacing string patterns. This task does not require a
# full parse tree, and having one makes the task considerably more complicated.
# See the accompanying README.md for more info.
comment: "--" (`[ @eqs=*`= `[ ..(`]eqs`]) % \n / ..$)
string: `"..`" % (`\.) / `'..`' % (`\.) / `[ @eqs=*`= `[ .. (`]eqs`]) % \n
table: `{..`} % (table/string/comment/\n)
keyword: |(
"and" / "break" / "do" / "else" / "elseif" / "end" / "false" / "for" /
"function" / "goto" / "if" / "in" / "local" / "nil" / "not" / "or" /
"repeat" / "return" / "then" / "true" / "until" / "while"
)|
function-def: |"function"|[_id (*(`.id)[`:id])]_ parens
block: function / |("do"/"then")| .. (|"end"|) % (comment/string/block/\n)
function: function-def .. (|"end"|) % (comment/string/block/\n)

30
grammars/python.bp Normal file
View File

@ -0,0 +1,30 @@
# Syntax definitions for some Python-specific patterns
#
# NOTE: this is *NOT* intended to be a complete definition of the language's
# syntax! Providing a full language grammar is overkill, because the intended
# use case is finding/replacing string patterns. This task does not require a
# full parse tree, and having one makes the task considerably more complicated.
# See the accompanying README.md for more info.
comment: `# ..$
string: quoted-string / `b quoted-string / `f quoted-string
quoted-string: "'''".."'''" % \n / '"""'..'"""' % \n / `"..`" % (`\.) / `'..`' % (`\.)
dict: `{__`} / `{ __ +(expr__`:__expr) % (__`,__) __ `}
set: `{ __ +expr % (__`,__) __ `}
list: `[ __ +expr % (__`,__) __ `]
keyword: |("and" / "as" / "assert" / "break" / "class" / "continue" / "def" /
"del" / "elif" / "else" / "except" / "finally" / "for" / "from" /
"global" / "if" / "import" / "in" / "is" / "lambda" / "None" / "nonlocal" /
"not" / "or" / "pass" / "raise" / "return" / "try" / "while" /
"with" / "yield")|
expr: (function-call / lambda / string / dict / set / list / id / number / `(__expr__`)) *(method/attr/index)
method: `. function-call
attr: `. id
index: `[__expr[__`:__expr [__`:__expr]]__`]
tuple: `(__`) / +(__expr__`,) [__expr]
empty-tuple: `(__`)
class: |"class"|_id`: +(\N ..$)
function: function-def +(\N ..$)
function-def: |"def"|_id parens `:
function-call: !<"def " id parens
lambda: |"lambda"| .. `:__expr