NXP: Natural eXpression Parsing
NXP is a parsing library written in Python 3, inspired by pyparsing and Microsoft Monarch.
It allows users to do two things:
- Define text patterns by combining Python objects, instead of writing complicated regular expressions.
- Define and parse complex languages, with a simple dictionary!
Is it really that simple?
Don't take my word for it; see for yourself with the examples below, and the notebooks in the examples/ folder. 😊
Examples
Matching HTML tags
This is a quick example to show how complex expressions are created in NXP, by combining Python objects.
from nxp import Seq, String, Either, Any, make_cursor
# property name, optionally assigned a value between quotes
attr = Seq( [r'\s+(\w+)', Seq([ r'\s*=\s*', String() ])], skip=1 )
# open/close tags, or self-closed tag
tag = Either(
Seq( [r'<(\w+)', Any(attr), r'\s*/?>'] ),
r'</(\w+)\s*>'
)
# create cursor and find matches
cur = make_cursor('''
Not <a><tag</a>
<input type="checkbox" value="42" checked>
<img src="foo/bar.jpg" />
''')
for m in tag.find(cur,multi=True): print(m.insitu(cur.buffer))
output:
Not <a><tag</a>
---
Not <a><tag</a>
----
<input type="checkbox" value="42" checked>
------------------------------------------
<img src="foo/bar.jpg" />
-------------------------
Parsing LaTeX-like commands
This is a quick example to illustrate parsing with NXP. We want to parse (very simplified) LaTeX-like patterns \command{ body } in the file foo.txt:
Inspirational quote:
\quote{
Time you enjoy wasting is \it{not} wasted time.
}
Command without a body \command, or with an empty one \command{}.
This is how to define a language to match such patterns in NXP:
import nxp
# define these rules separately so they can be re-used
backslash = [ r'\\\\', ('rep','\\') ]
command = [ r'\\(\w+)', ('open','command'), ('tag','cmd') ]
# create a parser
parser = nxp.make_parser({
'lang': {
'main': [
backslash, # replace escaped backslashes
command # open "command" scope if we find something like '\word'
],
'command': { # the "command" scope
'main': [
[ r'\{', ('open','command.body'), ('tag','body') ],
# open "body" subscope if command is followed by '{'
[ None, 'close' ]
# otherwise close the scope
],
'body': [ # the "command.body" scope
backslash,
[ r'\\\{', ('rep','{') ],
[ r'\\\}', ('rep','}') ],
# deal with escapes before looking for a nested command
command,
# look for nested commands
[ r'\}', ('tag','/body'), ('close',2) ]
# the command ends when the body ends: close both scopes
]
}
}
})
print(nxp.parsefile( parser, 'foo.txt' ))
and the output is a simple AST:
Scope("main"): 3 element(s)
[0] Scope("command"): 2 element(s)
[0] (1, 0) - (1, 6) \quote
[1] Scope("command.body"): 3 element(s)
[0] (1, 6) - (1, 7) {
[1] Scope("command"): 2 element(s)
[0] (2, 30) - (2, 33) \it
[1] Scope("command.body"): 2 element(s)
[0] (2, 33) - (2, 34) {
[1] (2, 37) - (2, 38) }
[2] (3, 0) - (3, 1) }
[1] Scope("command"): 1 element(s)
[0] (5, 23) - (5, 31) \command
[2] Scope("command"): 2 element(s)
[0] (5, 54) - (5, 62) \command
[1] Scope("command.body"): 2 element(s)
[0] (5, 62) - (5, 63) {
[1] (5, 63) - (5, 64) }
Metadata
Release files for nxp 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nxp-0.3.1.tar.gz | 27.1 kB | Without provenance | Details for nxp-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nxp-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for nxp-0.3.1-py3-none-any.whl |
Total release size: 65.2 kB
Release files / nxp-0.3.1.tar.gz
| Download URL | nxp-0.3.1.tar.gz |
|---|---|
| Size | 27.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49b6470ace8738dbc9e946196096c51fabb713f47dbeef08eb2db3197342f526
|
|
BLAKE2b-256 checksum How to use checksums |
492eba1730f14ece6bc42b8d3a66271ab262430a71254022873d0b383e4e620c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8
|
Release files / nxp-0.3.1-py3-none-any.whl
| Download URL | nxp-0.3.1-py3-none-any.whl |
|---|---|
| Size | 38.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5211ad10d39e21d7a080d7d3301af676f9f9d6076ad4e123669f7a6d85015d2f
|
|
BLAKE2b-256 checksum How to use checksums |
d5c339d7f7865f35f040b253bd93e42f7f1e64cea92873b9517a591f9dcd1873
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8
|
Security for nxp 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.