Create Parser.optional() helper method
This commit is contained in:
@@ -42,13 +42,6 @@ class TypeNode(AccessNode):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _parse(cls, input):
|
def _parse(cls, input):
|
||||||
return Parser.oneOf(
|
|
||||||
cls._specifiedTypeParser(),
|
|
||||||
cls._rawTypeParser()
|
|
||||||
)(input)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _specifiedTypeParser(cls):
|
|
||||||
def createNode(type, specifier):
|
def createNode(type, specifier):
|
||||||
node = TypeNode(type.pos)
|
node = TypeNode(type.pos)
|
||||||
node.type = type.value
|
node.type = type.value
|
||||||
@@ -57,9 +50,9 @@ class TypeNode(AccessNode):
|
|||||||
|
|
||||||
return Parser.allOf(
|
return Parser.allOf(
|
||||||
cls._rawTypeParser(),
|
cls._rawTypeParser(),
|
||||||
TypeSpecifier.parse,
|
Parser.optional(TypeSpecifier.parse),
|
||||||
createNode=createNode
|
createNode=createNode
|
||||||
)
|
)(input)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _rawTypeParser(cls):
|
def _rawTypeParser(cls):
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from smnp.ast.node.ignore import IgnoredNode
|
from smnp.ast.node.ignore import IgnoredNode
|
||||||
from smnp.ast.node.model import ParseResult, Node
|
from smnp.ast.node.model import ParseResult, Node
|
||||||
|
from smnp.ast.node.none import NoneNode
|
||||||
from smnp.error.syntax import SyntaxException
|
from smnp.error.syntax import SyntaxException
|
||||||
|
|
||||||
|
|
||||||
@@ -139,3 +140,14 @@ class Parser:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
return parse
|
return parse
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def optional(parser):
|
||||||
|
def parse(input):
|
||||||
|
result = parser(input)
|
||||||
|
if result.result:
|
||||||
|
return result
|
||||||
|
|
||||||
|
return ParseResult.OK(NoneNode())
|
||||||
|
|
||||||
|
return parse
|
||||||
|
|||||||
Reference in New Issue
Block a user