mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 13:24:39 +08:00
Document pgen grammars a bit better
This commit is contained in:
@@ -5,6 +5,19 @@
|
|||||||
# Copyright David Halter and Contributors
|
# Copyright David Halter and Contributors
|
||||||
# Modifications are dual-licensed: MIT and PSF.
|
# Modifications are dual-licensed: MIT and PSF.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Specifying grammars in pgen is possible with this grammar::
|
||||||
|
|
||||||
|
grammar: (NEWLINE | rule)* ENDMARKER
|
||||||
|
rule: NAME ':' rhs NEWLINE
|
||||||
|
rhs: items ('|' items)*
|
||||||
|
items: item+
|
||||||
|
item: '[' rhs ']' | atom ['+' | '*']
|
||||||
|
atom: '(' rhs ')' | NAME | STRING
|
||||||
|
|
||||||
|
This grammar is self-referencing.
|
||||||
|
"""
|
||||||
|
|
||||||
from parso.pgen2.grammar import Grammar
|
from parso.pgen2.grammar import Grammar
|
||||||
from parso.python import token
|
from parso.python import token
|
||||||
from parso.python import tokenize
|
from parso.python import tokenize
|
||||||
@@ -155,11 +168,11 @@ class _GrammarParser():
|
|||||||
def _parse(self):
|
def _parse(self):
|
||||||
dfas = {}
|
dfas = {}
|
||||||
start_symbol = None
|
start_symbol = None
|
||||||
# MSTART: (NEWLINE | RULE)* ENDMARKER
|
# grammar: (NEWLINE | rule)* ENDMARKER
|
||||||
while self.type != token.ENDMARKER:
|
while self.type != token.ENDMARKER:
|
||||||
while self.type == token.NEWLINE:
|
while self.type == token.NEWLINE:
|
||||||
self._gettoken()
|
self._gettoken()
|
||||||
# RULE: NAME ':' RHS NEWLINE
|
# rule: NAME ':' rhs NEWLINE
|
||||||
name = self._expect(token.NAME)
|
name = self._expect(token.NAME)
|
||||||
self._expect(token.COLON)
|
self._expect(token.COLON)
|
||||||
a, z = self._parse_rhs()
|
a, z = self._parse_rhs()
|
||||||
@@ -260,7 +273,7 @@ class _GrammarParser():
|
|||||||
break
|
break
|
||||||
|
|
||||||
def _parse_rhs(self):
|
def _parse_rhs(self):
|
||||||
# RHS: ALT ('|' ALT)*
|
# rhs: items ('|' items)*
|
||||||
a, z = self._parse_alt()
|
a, z = self._parse_alt()
|
||||||
if self.value != "|":
|
if self.value != "|":
|
||||||
return a, z
|
return a, z
|
||||||
@@ -277,7 +290,7 @@ class _GrammarParser():
|
|||||||
return aa, zz
|
return aa, zz
|
||||||
|
|
||||||
def _parse_alt(self):
|
def _parse_alt(self):
|
||||||
# ALT: ITEM+
|
# items: item+
|
||||||
a, b = self._parse_item()
|
a, b = self._parse_item()
|
||||||
while (self.value in ("(", "[") or
|
while (self.value in ("(", "[") or
|
||||||
self.type in (token.NAME, token.STRING)):
|
self.type in (token.NAME, token.STRING)):
|
||||||
@@ -287,7 +300,7 @@ class _GrammarParser():
|
|||||||
return a, b
|
return a, b
|
||||||
|
|
||||||
def _parse_item(self):
|
def _parse_item(self):
|
||||||
# ITEM: '[' RHS ']' | ATOM ['+' | '*']
|
# item: '[' rhs ']' | atom ['+' | '*']
|
||||||
if self.value == "[":
|
if self.value == "[":
|
||||||
self._gettoken()
|
self._gettoken()
|
||||||
a, z = self._parse_rhs()
|
a, z = self._parse_rhs()
|
||||||
@@ -307,7 +320,7 @@ class _GrammarParser():
|
|||||||
return a, a
|
return a, a
|
||||||
|
|
||||||
def _parse_atom(self):
|
def _parse_atom(self):
|
||||||
# ATOM: '(' RHS ')' | NAME | STRING
|
# atom: '(' rhs ')' | NAME | STRING
|
||||||
if self.value == "(":
|
if self.value == "(":
|
||||||
self._gettoken()
|
self._gettoken()
|
||||||
a, z = self._parse_rhs()
|
a, z = self._parse_rhs()
|
||||||
|
|||||||
Reference in New Issue
Block a user