Skip to content

Instantly share code, notes, and snippets.

@omerfarukz

omerfarukz/URI.g4

Last active Jun 11, 2021
Embed
What would you like to do?
// Simple URI parser with ANTLR4
// Omer Faruk Zorlu, Jun 2021
grammar URI;
prog : (segment)+ ;
segment : protocol? host path? ;
protocol : VALID_CHARSET+ PROTOCOL_DELIMITER ;
host : VALID_CHARSET+ ~'/'? ;
path : (VALID_CHARSET | PATH_DELIMITER)+ query? ;
query : VALID_CHARSET? QUERY_DELIMITER query_name_value ;
query_name_value : ((VALID_CHARSET (QUERY_NVP_DELIMITER VALID_CHARSET)?) QUERY_PRM_DELIMITER?)+ ;
QUERY_DELIMITER : '?' ;
QUERY_PRM_DELIMITER : '&' ;
QUERY_NVP_DELIMITER : '=' ;
PATH_DELIMITER : '/' ;
PROTOCOL_DELIMITER : '://' '/'? ;
VALID_CHARSET : (ALPHA | DIGIT | '.' | '-')+ ;
EOL : EOF | '\r'? '\n' ;
fragment ALPHA : [a-zA-Z] ;
fragment DIGIT : '0'| [1-9] ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment