Skip to content

Commit

Permalink
Initial lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
amykyta3 committed Feb 11, 2019
0 parents commit f3d2e89
Show file tree
Hide file tree
Showing 6 changed files with 805 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/__pycache__
build/
dist/
*.egg-info/
*.eggs
.coverage
**/.vscode
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pygments-systemrdl.svg)](https://pypi.org/project/pygments-systemrdl)

# pygments-systemrdl

This module implements a [SystemRDL 2.0](http://accellera.org/downloads/standards/systemrdl)
language lexer for [Pygments](https://pytments.org).
The lexer is published as an entry point and, once installed, Pygments will
pick it up automatically.

The language is registered under the alias: `systemrdl`

## Installation

Install from [PyPi](https://pypi.org/project/pygments-systemrdl) using pip:

python3 -m pip install pygments-systemrdl
Empty file added rdl_pygments/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions rdl_pygments/rdllexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pygments import lexer
from pygments import token
from pygments.lexers.perl import PerlLexer

class SystemRDLLexer(lexer.RegexLexer):
name = 'SystemRDL'
aliases = ['systemrdl']
filenames = ['*.rdl']

tokens = {

'root': [
lexer.include('comments'),
lexer.include('perl'),
lexer.include('prop-assign'),
lexer.include('comp-def'),

(lexer.words((
'addrmap', 'regfile', 'reg', 'field', 'enum', 'struct',
'constraint', 'signal', 'mem'
), suffix=r'\b'), token.Keyword),

(lexer.words((
'external', 'abstract', 'alias', 'unsigned'
), suffix=r'\b'), token.Keyword),

(lexer.words((
'bit', 'boolean', 'onreadtype', 'onwritetype', 'string',
'accesstype', 'addressingtype', 'component',
), suffix=r'\b'), token.Keyword.Type),

lexer.include('literals'),
(r'[{}()\[\],.;\']', token.Punctuation),
(r'[~!%^&*+-=|?:<>/-@]', token.Operator),
(r'[a-zA-Z][\w]*', token.Name),
(r'\s', token.Text)
],

'comments': [
(r'(?s)/\*.*\*/', token.Comment.Multiline),
(r'//.*?$', token.Comment.Single),
],

'perl': [
(r'(?s)(<%=?)(.+?)(%>)', lexer.bygroups(token.Name.Tag, lexer.using(PerlLexer), token.Name.Tag)),
],

'literals': [
(r'([0-9]+)?(\'h)[0-9a-fA-F_]+', token.Number.Hex),
(r'([0-9]+)?(\'b)[01_]+', token.Number.Bin),
(r'([0-9]+)?(\'d)[0-9_]+', token.Number.Integer),
(r'([0-9]+)?(\'o)[0-7_]+', token.Number.Oct),
(r'0[xX][0-9a-fA-F_]+', token.Number.Hex),
(lexer.words(('true', 'false'), suffix=r'\b'), token.Literal),
(r'\d+', token.Number.Integer),
(r'"', token.String, 'string'),
],
'string': [
(r'"', token.String, '#pop'),
(r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', token.String.Escape),
(r'[^\\"\n]+', token.String), # all other characters
(r'\\\n', token.String), # line continuation
(r'\\', token.String), # stray backslash
],

'prop-assign': [
#(r'(\w+)\s*(;)', lexer.bygroups(token.Name.Attribute, token.Operator)),
(r'(\w+)(\s*)(=)', lexer.bygroups(token.Name.Attribute, token.Text, token.Operator)),
(r'(->)(\s*)(\w+)(\s*)(=)', lexer.bygroups(
token.Operator, token.Text, token.Name.Attribute, token.Text, token.Operator
)),
],

'comp-def': [
(r'(addrmap|regfile|reg|field|mem|signal)(\s+)([a-zA-Z][\w]*)', lexer.bygroups(token.Keyword, token.Text, token.Name.Class))
],
}
31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup (
name='pygments-systemrdl',
version="1.0.0",
author="Alex Mykyta",
author_email="[email protected]",
description="SystemRDL 2.0 lexer extension for Pygments",
long_description=long_description,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
install_requires=["pygments"],
entry_points =
"""
[pygments.lexers]
rdllexer = rdl_pygments.rdllexer:SystemRDLLexer
""",
classifiers=(
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
),
project_urls={
"Source": "https://github.com/SystemRDL/systemrdl-pygments",
},
)

0 comments on commit f3d2e89

Please sign in to comment.