MCPcopy Create free account
hub / github.com/SplootCode/splootcode / parseLeafToPyright

Function parseLeafToPyright

packages/language-python/src/nodes/utils.ts:81–127  ·  view source on GitHub ↗
(
  parseMapper: ParseMapper,
  tokens: SplootNode[],
  currentIndex: number
)

Source from the content-addressed store, hash-verified

79}
80
81function parseLeafToPyright(
82 parseMapper: ParseMapper,
83 tokens: SplootNode[],
84 currentIndex: number
85): [boolean, number, ExpressionNode] {
86 if (currentIndex >= tokens.length) {
87 // No tokens left when a leaf was expected
88 const errorExpr: ErrorNode = {
89 nodeType: ParseNodeType.Error,
90 category: ErrorExpressionCategory.MissingExpression,
91 id: parseMapper.getNextId(),
92 start: 0,
93 length: 0,
94 }
95 return [false, currentIndex, errorExpr]
96 }
97 const lookahead = tokens[currentIndex]
98 if (lookahead.type === 'PYTHON_BINARY_OPERATOR') {
99 const op = (lookahead as PythonBinaryOperator).getOperator()
100 if (op in UnaryOperators) {
101 // Don't care if it's invalid, it'll return an error node if needed
102 const [, leafIndex, operandNode] = parseLeafToPyright(parseMapper, tokens, currentIndex + 1)
103 const lhs: UnaryOperationNode = {
104 nodeType: ParseNodeType.UnaryOperation,
105 expression: operandNode,
106 id: parseMapper.getNextId(),
107 length: 0,
108 start: 0,
109 operator: UnaryOperators[op].type,
110 operatorToken: { start: 0, length: 0, type: UnaryOperators[op].tokenType },
111 }
112 operandNode.parent = lhs
113 return parseExpressionToPyright(parseMapper, tokens, lhs, leafIndex, UnaryOperators[op]['precedence'])
114 }
115 const errorExpr: ErrorNode = {
116 nodeType: ParseNodeType.Error,
117 category: ErrorExpressionCategory.MissingExpression,
118 id: parseMapper.getNextId(),
119 start: 0,
120 length: 0,
121 }
122 return [false, currentIndex, errorExpr]
123 }
124 // Consume one token - whatever it was it wasn't an operator
125 const leafNode = parseTokenToPyright(parseMapper, lookahead)
126 return [true, currentIndex + 1, leafNode]
127}
128
129function parseExpressionToPyright(
130 parseMapper: ParseMapper,

Callers 2

parseExpressionToPyrightFunction · 0.85
parseToPyrightFunction · 0.85

Calls 4

parseExpressionToPyrightFunction · 0.85
parseTokenToPyrightFunction · 0.85
getNextIdMethod · 0.80
getOperatorMethod · 0.45

Tested by

no test coverage detected