|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# This code is part of Qiskit. |
| 4 | +# |
| 5 | +# (C) Copyright IBM 2020. |
| 6 | +# |
| 7 | +# This code is licensed under the Apache License, Version 2.0. You may |
| 8 | +# obtain a copy of this license in the LICENSE.txt file in the root directory |
| 9 | +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | +# |
| 11 | +# Any modifications or derivative works of this code must retain this |
| 12 | +# copyright notice, and modified files need to carry a notice indicating |
| 13 | +# that they have been altered from the originals. |
| 14 | +"""Pygments tools for Qasm. |
| 15 | +""" |
| 16 | +from pygments.lexer import RegexLexer |
| 17 | +from pygments.token import (Comment, String, Keyword, |
| 18 | + Name, Number, Text) |
| 19 | +from pygments.style import Style |
| 20 | + |
| 21 | + |
| 22 | +class QasmTerminalStyle(Style): |
| 23 | + """A style for OpenQasm in a Terminal env (e.g. Jupyter print) |
| 24 | + """ |
| 25 | + styles = { |
| 26 | + String: 'ansibrightred', |
| 27 | + Number: 'ansibrightcyan', |
| 28 | + Keyword.Reserved: 'ansibrightgreen', |
| 29 | + Keyword.Declaration: 'ansibrightgreen', |
| 30 | + Keyword.Type: 'ansibrightmagenta', |
| 31 | + Name.Builtin: 'ansibrightblue', |
| 32 | + Name.Function: 'ansibrightyellow'} |
| 33 | + |
| 34 | + |
| 35 | +class QasmHTMLStyle(Style): |
| 36 | + """A style for OpenQasm in a HTML env (e.g. Jupyter widget) |
| 37 | + """ |
| 38 | + styles = { |
| 39 | + String: 'ansired', |
| 40 | + Number: 'ansicyan', |
| 41 | + Keyword.Reserved: 'ansigreen', |
| 42 | + Keyword.Declaration: 'ansigreen', |
| 43 | + Keyword.Type: 'ansimagenta', |
| 44 | + Name.Builtin: 'ansiblue', |
| 45 | + Name.Function: 'ansiyellow'} |
| 46 | + |
| 47 | + |
| 48 | +class OpenQASMLexer(RegexLexer): |
| 49 | + """A pygments lexer for OpenQasm |
| 50 | + """ |
| 51 | + name = 'OpenQASM' |
| 52 | + aliases = ['qasm'] |
| 53 | + filenames = ['*.qasm'] |
| 54 | + |
| 55 | + gates = ['id', 'cx', 'x', 'y', 'z', 's', 'sdg', 'h', |
| 56 | + 't', 'tdg', 'ccx', 'rx', 'ry', 'rz', |
| 57 | + 'cz', 'cy', 'ch', 'swap', 'cswap', 'crx', |
| 58 | + 'cry', 'crz', 'cu1', 'cu3', 'rxx', 'rzz', |
| 59 | + 'rccx', 'rcccx', 'u1', 'u2', 'u3'] |
| 60 | + |
| 61 | + tokens = { |
| 62 | + 'root': [ |
| 63 | + (r'\n', Text), |
| 64 | + (r'[^\S\n]+', Text), |
| 65 | + (r'//\n', Comment), |
| 66 | + (r'//.*?$', Comment.Single), |
| 67 | + |
| 68 | + # Keywords |
| 69 | + (r'(OPENQASM|include)\b', Keyword.Reserved, 'keywords'), |
| 70 | + (r'(qreg|creg)\b', Keyword.Declaration), |
| 71 | + |
| 72 | + # Treat 'if' special |
| 73 | + (r'(if)\b', Keyword.Reserved, 'if_keywords'), |
| 74 | + |
| 75 | + # Constants |
| 76 | + (r'(pi)\b', Name.Constant), |
| 77 | + |
| 78 | + # Special |
| 79 | + (r'(barrier|measure|reset)\b', Name.Builtin, 'params'), |
| 80 | + |
| 81 | + # Gates (Types) |
| 82 | + ('(' + '|'.join(gates) + r')\b', Keyword.Type, 'params'), |
| 83 | + (r'[unitary\d+]', Keyword.Type), |
| 84 | + |
| 85 | + # Functions |
| 86 | + (r'(gate)\b', Name.Function, 'gate'), |
| 87 | + |
| 88 | + # Generic text |
| 89 | + (r"[a-zA-Z_][a-zA-Z0-9_]*", Text, 'index')], |
| 90 | + |
| 91 | + 'keywords': [(r'\s*("([^"]|"")*")', String, '#push'), |
| 92 | + (r"\d+", Number, '#push'), |
| 93 | + (r'.*\(', Text, 'params')], |
| 94 | + |
| 95 | + 'if_keywords': [(r'[a-zA-Z0-9_]*', String, '#pop'), |
| 96 | + (r"\d+", Number, '#push'), |
| 97 | + (r'.*\(', Text, 'params')], |
| 98 | + |
| 99 | + 'params': [(r"[a-zA-Z_][a-zA-Z0-9_]*", Text, '#push'), |
| 100 | + (r'\d+', Number, '#push'), |
| 101 | + (r'(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?', Number, '#push'), |
| 102 | + (r'\)', Text)], |
| 103 | + |
| 104 | + 'gate': [(r'[unitary\d+]', Keyword.Type, '#push'), |
| 105 | + (r'p\d+', Text, '#push')], |
| 106 | + |
| 107 | + 'index': [(r"\d+", Number, '#pop')] |
| 108 | + } |
0 commit comments