squiggle/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_IncludeParser.peggy

49 lines
1.0 KiB
Plaintext
Raw Normal View History

// Includes are at the start of the file, before any other code.
// There might be some comments before or between the includes.
// #include "string"
2022-08-31 21:54:54 +00:00
// #include "string2" as aVariable
start
2022-08-23 09:47:24 +00:00
= (newLine/_/comment/delimitedComment)* @includes newLine* ignore
includes
2022-08-31 21:54:54 +00:00
= head:dependencyStatement tail:(newLine+ @dependencyStatement)*
{return [head, ...tail].filter( e => e != '');}
2022-08-31 21:54:54 +00:00
/ ! '#'
{return [];}
2022-08-31 21:54:54 +00:00
dependencyStatement
= includeStatement
/ comment
/ delimitedComment
2022-08-31 21:54:54 +00:00
includeStatement
= _* '#include' _+ file:string variable:(_+ 'as' _+ @identifier)? _* & newLine
{return [!variable ? '' : variable, file]}
string 'string'
= characters:("'" @([^'])* "'") {return characters.join('');}
/ characters:('"' @([^"])* '"') {return characters.join('');}
ignore = (any / newLine / _)*
2022-08-31 21:54:54 +00:00
comment 'comment'
= '//'any* & newLine
{ return '';}
2022-08-31 21:54:54 +00:00
delimitedComment 'comment'
= '/*' ([^*]*) '*/'
{ return '';}
_ "white space"
= [ \t]
newLine "newline"
= [\n\r]
2022-08-31 21:54:54 +00:00
any 'code'
= [^\r\n]
identifier 'identifier'
= ([_a-z]+[_a-z0-9]i*) {return text();}