aboutsummaryrefslogtreecommitdiff
path: root/grammars
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-01-17 19:42:11 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-01-17 19:42:11 -0800
commit51313c4773f0af62121b2192ff0cca9560b7ab44 (patch)
tree5873f54aafe2cd537e286a88b368ba0315d3e15d /grammars
parent23f9b7ade9eccc6aee2e6c2a6737630a9d0cc77d (diff)
Adding more language grammars
Diffstat (limited to 'grammars')
-rw-r--r--grammars/README.md14
-rw-r--r--grammars/bp.bp (renamed from grammars/bpeg.bp)5
-rw-r--r--grammars/c.bp23
-rw-r--r--grammars/lua.bp19
-rw-r--r--grammars/python.bp30
5 files changed, 91 insertions, 0 deletions
diff --git a/grammars/README.md b/grammars/README.md
new file mode 100644
index 0000000..7fa486f
--- /dev/null
+++ b/grammars/README.md
@@ -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`.
diff --git a/grammars/bpeg.bp b/grammars/bp.bp
index 0894e76..7379dfe 100644
--- a/grammars/bpeg.bp
+++ b/grammars/bp.bp
@@ -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 __ `: __ (
diff --git a/grammars/c.bp b/grammars/c.bp
new file mode 100644
index 0000000..2b2e1d9
--- /dev/null
+++ b/grammars/c.bp
@@ -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..$)
diff --git a/grammars/lua.bp b/grammars/lua.bp
new file mode 100644
index 0000000..7ffdc73
--- /dev/null
+++ b/grammars/lua.bp
@@ -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)
diff --git a/grammars/python.bp b/grammars/python.bp
new file mode 100644
index 0000000..0683c0a
--- /dev/null
+++ b/grammars/python.bp
@@ -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