parseEntityStatement method parses an ENTITY statement and returns an EntityStatement AST node
()
| 228 | |
| 229 | // parseEntityStatement method parses an ENTITY statement and returns an EntityStatement AST node |
| 230 | func (p *Parser) parseEntityStatement() (*ast.EntityStatement, error) { |
| 231 | // create a new EntityStatement object and set its Entity field to the currentToken |
| 232 | stmt := &ast.EntityStatement{Entity: p.currentToken} |
| 233 | // expect the next token to be an identifier token, and set the EntityStatement's Name field to the identifier's value |
| 234 | if !p.expectAndNext(token.IDENT) { |
| 235 | return nil, p.Error() |
| 236 | } |
| 237 | stmt.Name = p.currentToken |
| 238 | |
| 239 | // add the entity reference to the Parser's entityReferences map |
| 240 | err := p.references.AddEntityReference(stmt.Name.Literal) |
| 241 | if err != nil { |
| 242 | p.duplicationError(stmt.Name.Literal) // Generate an error message indicating a duplication error |
| 243 | return nil, p.Error() |
| 244 | } |
| 245 | |
| 246 | // expect the next token to be a left brace token, indicating the start of the entity's body |
| 247 | if !p.expectAndNext(token.LCB) { |
| 248 | return nil, p.Error() |
| 249 | } |
| 250 | |
| 251 | // loop through the entity's body until a right brace token is encountered |
| 252 | for !p.currentTokenIs(token.RCB) { |
| 253 | // if the currentToken is EOF, raise an error and return nil for both the statement and error values |
| 254 | if p.currentTokenIs(token.EOF) { |
| 255 | p.currentError(token.RCB) |
| 256 | return nil, p.Error() |
| 257 | } |
| 258 | // based on the currentToken's type, parse a RelationStatement or PermissionStatement and add it to the EntityStatement's corresponding field |
| 259 | switch p.currentToken.Type { |
| 260 | case token.RELATION: |
| 261 | relation, err := p.parseRelationStatement(stmt.Name.Literal) |
| 262 | if err != nil { |
| 263 | return nil, p.Error() |
| 264 | } |
| 265 | stmt.RelationStatements = append(stmt.RelationStatements, relation) |
| 266 | case token.ATTRIBUTE: |
| 267 | attribute, err := p.parseAttributeStatement(stmt.Name.Literal) |
| 268 | if err != nil { |
| 269 | return nil, p.Error() |
| 270 | } |
| 271 | stmt.AttributeStatements = append(stmt.AttributeStatements, attribute) |
| 272 | case token.PERMISSION: |
| 273 | action, err := p.parsePermissionStatement(stmt.Name.Literal) |
| 274 | if err != nil { |
| 275 | return nil, p.Error() |
| 276 | } |
| 277 | stmt.PermissionStatements = append(stmt.PermissionStatements, action) |
| 278 | default: |
| 279 | // if the currentToken is not recognized, check if it is a newline, left brace, or right brace token, and skip it if it is |
| 280 | if !p.currentTokenIs(token.NEWLINE) && !p.currentTokenIs(token.LCB) && !p.currentTokenIs(token.RCB) { |
| 281 | // if the currentToken is not recognized and not a newline, left brace, or right brace token, raise an error and return nil for both the statement and error values |
| 282 | p.currentError(token.RELATION, token.PERMISSION, token.ATTRIBUTE) |
| 283 | return nil, p.Error() |
| 284 | } |
| 285 | } |
| 286 | // move to the next token in the input string |
| 287 | p.next() |
no test coverage detected