<commit_before>grammar Express;

schema_declaration
	: SCHEMA Version ';' type_declaration* entity_declaration* function_declaration*  END_SCHEMA';' EOF;

type_declaration 
	: TYPE type_name EQ (value_type|enumeration|select) FIXED? ';' type_declaration_body? END_TYPE ';' ;

type_name	
	: Identifier
	;

value_type 
	: BOOLEAN
	| INTEGER
	| LOGICAL
	| REAL
	| STRING
	| STRING_SIZED
	| set_declaration
	| list_declaration
	| array_declaration
	| Identifier
	;

set_declaration
	: SET '[' Integer COLON (set_size|'?') ']' OF value_type (FOR Identifier)?
	;

set_size
	: Integer
	;

list_declaration
	: LIST '[' Integer COLON (list_size|'?') ']' OF value_type
	;

list_size
	: Integer
	;

array_declaration
	: ARRAY '[' Integer COLON (array_size|'?') ']' OF value_type
	;

array_size
	: Integer
	;

enumeration
	: ENUMERATION OF LP enum_id_list RP 
	;

enum_id_list
	: id_list
	;

select
	: SELECT LP select_id_list RP
	;

select_id_list
	: id_list
	;

id_list
	: Identifier(','Identifier)*
	| IfcType(','IfcType)*
	;

type_declaration_body 
	: rule_declaration 
	;

rule_declaration : 'WHERE' rule+ ;

rule 
	: Identifier COLON expr ';'
	;

expr
	: func_call_expr
	| bool_expr
	| LB bool_expr RB
	; 

func_call_expr
	: (EXISTS|SIZEOF|TYPEOF|QUERY|ABS|USEDIN|Identifier)'(' func_parameters ')'
	;

func_parameters
	: atom(','atom)*
	| formula_expr
	;

query_expr
	: Identifier INIT (Identifier|Path|PropertyAccessor|func_call_expr) PIPE bool_expr
	;

bool_expr
	: atom((LT|GT|LTE|GTE|EQ|NEQ|AND|OR)atom)*
	| (IfcType|Path|PropertyAccessor|Identifier|SELF) IN (func_call_expr|'['id_list']')
	;

formula_expr
	: atom((ADD|SUB|MUL|DIV)atom)+
	;

atom 
	: SELF
	| IfcType
	| Integer 
	| Float
	| Identifier
	| Path
	| PropertyAccessor
	| SetAccessor
	| '[' id_list ']'
	| query_expr
	| func_call_expr
	| '('NOT? expr ')'
	| NOT?'('expr')'
	;

self_property
	: SELF '.' Identifier
	;

entity_declaration
	: ENTITY entity_name ';'? entity_declaration_body END_ENTITY ';' 
	;

entity_name
	: Identifier
	;

entity_declaration_body
	: attribute* supertype_declaration? subtype_declaration? inverse_declaration? derive_declaration? rule_declaration? unique_declaration? 
	;

supertype_declaration 
	:  ABSTRACT? SUPERTYPE OF LP (one_of | supertype_name) RP ';'? attribute* 
	;

supertype_name
	: Identifier
	;

subtype_declaration 
	: SUBTYPE OF LP (one_of | subtype_name) RP ';' attribute* 
	;

subtype_name
	: Identifier
	;

attribute
	: attribute_name COLON OPTIONAL? value_type definition? ';'
	;

attribute_name
	: Identifier
	;

definition
	: DEF expr;

one_of 
	: ONEOF LP id_list RP 
	;

inverse_declaration
	: INVERSE attribute+ 
	;

derive_declaration
	: DERIVE attribute+
	;

unique_declaration
	: UNIQUE unique_statement+ 
	;

unique_statement
	: Identifier COLON .*? ';' 
	;

function_declaration 
	: 'FUNCTION' function_declaration_body 'END_FUNCTION' ;

function_declaration_body
	: .*? ;

// Lexer

//Base Types
BOOLEAN : 'BOOLEAN' ;
INTEGER : 'INTEGER' ;
LIST : 'LIST';<commit_msg>Remove underscores from rule names.<commit_after>grammar Express;

schemaDeclaration
	: SCHEMA Version ';' typeDeclaration* entityDeclaration* functionDeclaration*  END_SCHEMA';' EOF;

typeDeclaration 
	: TYPE typeName EQ (valueType|enumeration|select) FIXED? ';' typeDeclarationBody? END_TYPE ';' ;

typeName	
	: Identifier
	;

valueType 
	: BOOLEAN
	| INTEGER
	| LOGICAL
	| REAL
	| STRING
	| STRING_SIZED
	| setDeclaration
	| listDeclaration
	| arrayDeclaration
	| Identifier
	;

setDeclaration
	: SET '[' Integer COLON (setSize|'?') ']' OF valueType (FOR Identifier)?
	;

setSize
	: Integer
	;

listDeclaration
	: LIST '[' Integer COLON (listSize|'?') ']' OF valueType
	;

listSize
	: Integer
	;

arrayDeclaration
	: ARRAY '[' Integer COLON (arraySize|'?') ']' OF valueType
	;

arraySize
	: Integer
	;

enumeration
	: ENUMERATION OF LP enumIdList RP 
	;

enumIdList
	: idList
	;

select
	: SELECT LP selectIdList RP
	;

selectIdList
	: idList
	;

idList
	: Identifier(','Identifier)*
	| IfcType(','IfcType)*
	;

typeDeclarationBody 
	: ruleDeclaration 
	;

ruleDeclaration : 'WHERE' rule+ ;

rule 
	: Identifier COLON expr ';'
	;

expr
	: funcCallExpr
	| boolExpr
	| LB boolExpr RB
	; 

funcCallExpr
	: (EXISTS|SIZEOF|TYPEOF|QUERY|ABS|USEDIN|Identifier)'(' funcParameters ')'
	;

funcParameters
	: atom(','atom)*
	| formulaExpr
	;

queryExpr
	: Identifier INIT (Identifier|Path|PropertyAccessor|funcCallExpr) PIPE boolExpr
	;

boolExpr
	: atom((LT|GT|LTE|GTE|EQ|NEQ|AND|OR)atom)*
	| (IfcType|Path|PropertyAccessor|Identifier|SELF) IN (funcCallExpr|'['idList']')
	;

formulaExpr
	: atom((ADD|SUB|MUL|DIV)atom)+
	;

atom 
	: SELF
	| IfcType
	| Integer 
	| Float
	| Identifier
	| Path
	| PropertyAccessor
	| SetAccessor
	| '[' idList ']'
	| queryExpr
	| funcCallExpr
	| '('NOT? expr ')'
	| NOT?'('expr')'
	;

selfProperty
	: SELF '.' Identifier
	;

entityDeclaration
	: ENTITY entityName ';'? entityDeclarationBody END_ENTITY ';' 
	;

entityName
	: Identifier
	;

entityDeclarationBody
	: attribute* supertypeDeclaration? subtypeDeclaration? inverseDeclaration? deriveDeclaration? ruleDeclaration? uniqueDeclaration? 
	;

supertypeDeclaration 
	:  ABSTRACT? SUPERTYPE OF LP (oneOf | supertypeName) RP ';'? attribute* 
	;

supertypeName
	: Identifier
	;

subtypeDeclaration 
	: SUBTYPE OF LP (oneOf | subtypeName) RP ';' attribute* 
	;

subtypeName
	: Identifier
	;

attribute
	: attributeName COLON OPTIONAL? valueType definition? ';'
	;

attributeName
	: Identifier
	;

definition
	: DEF expr;

oneOf 
	: ONEOF LP idList RP 
	;

inverseDeclaration
	: INVERSE attribute+ 
	;

deriveDeclaration
	: DERIVE attribute+
	;

uniqueDeclaration
	: UNIQUE uniqueStatement+ 
	;

uniqueStatement
	: Identifier COLON .*? ';' 
	;

functionDeclaration 
	: 'FUNCTION' functionDeclarationBody 'END_FUNCTION' ;

functionDeclarationBody
	: .*? ;

// Lexer

//Base Types
BOOLEAN : 'BOOLEAN' ;
INTEGER : 'INTEGER' ;
LIST : 'LIST';
{'fraction_non_alphanumeric': 0.11681926076636148, 'fraction_numerical': 0.0, 'mean_word_length': 4.544172932330827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1167571', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2265, 'n_words': 419}