robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#pragma once 2 3#include <stdbool.h> 4#include <stddef.h> 5#include <stdint.h> 6 7#include "nvim/eval/typval_defs.h" 8#include "nvim/types_defs.h" 9#include "nvim/viml/parser/parser_defs.h" 10 11// Defines whether to ignore case: 12// == kCCStrategyUseOption 13// ==# kCCStrategyMatchCase 14// ==? kCCStrategyIgnoreCase 15typedef enum { 16kCCStrategyUseOption = 0 ,// 0 for xcalloc 17kCCStrategyMatchCase = '#' , 18kCCStrategyIgnoreCase = '?' , 19}ExprCaseCompareStrategy ; 20 21/// Lexer token type 22typedef enum { 23kExprLexInvalid = 0 ,///< Invalid token, indicaten an error. 24kExprLexMissing ,///< Missing token, for use in parser. 25kExprLexSpacing ,///< Spaces, tabs, newlines, etc. 26kExprLexEOC ,///< End of command character: NL, |, just end of stream. 27 28kExprLexQuestion ,///< Question mark, for use in ternary. 29kExprLexColon ,///< Colon, for use in ternary. 30kExprLexOr ,///< Logical or operator. 31kExprLexAnd ,///< Logical and operator. 32kExprLexComparison ,///< One of the comparison operators. 33kExprLexPlus ,///< Plus sign. 34kExprLexMinus ,///< Minus sign. 35kExprLexDot ,///< Dot: either concat or subscript, also part of the float. 36kExprLexMultiplication ,///< Multiplication, division or modulo operator. 37 38kExprLexNot ,///< Not: !. 39 40kExprLexNumber ,///< Integer number literal, or part of a float. 41kExprLexSingleQuotedString ,///< Single quoted string literal. 42kExprLexDoubleQuotedString ,///< Double quoted string literal. 43kExprLexOption ,///< &optionname option value. 44kExprLexRegister ,///< @r register value. 45kExprLexEnv ,///< Environment $variable value. 46kExprLexPlainIdentifier ,///< Identifier without scope: `abc`, `foo#bar`. 47 48kExprLexBracket ,///< Bracket, either opening or closing. 49kExprLexFigureBrace ,///< Figure brace, either opening or closing. 50kExprLexParenthesis ,///< Parenthesis, either opening or closing. 51kExprLexComma ,///< Comma. 52kExprLexArrow ,///< Arrow, like from lambda expressions. 53kExprLexAssignment ,///< Assignment: `=` or `{op}=`. 54// XXX When modifying this enum you need to also modify eltkn_type_tab in 55// expressions.c and tests and, possibly, viml_pexpr_repr_token. 56}LexExprTokenType ; 57 58typedef enum { 59kExprCmpEqual ,///< Equality, inequality. 60kExprCmpMatches ,///< Matches regex, not matches regex. 61kExprCmpGreater ,///< `>` or `<=` 62kExprCmpGreaterOrEqual ,///< `>=` or `<`. 63kExprCmpIdentical ,///< `is` or `isnot` 64}ExprComparisonType ; 65 66/// All possible option scopes 67typedef enum { 68kExprOptScopeUnspecified = 0 , 69kExprOptScopeGlobal = 'g' , 70kExprOptScopeLocal = 'l' , 71}ExprOptScope ; 72 73/// All possible assignment types: `=` and `{op}=`. 74typedef enum { 75kExprAsgnPlain = 0 ,///< Plain assignment: `=`. 76kExprAsgnAdd ,///< Assignment augmented with addition: `+=`. 77kExprAsgnSubtract ,///< Assignment augmented with subtraction: `-=`. 78kExprAsgnConcat ,///< Assignment augmented with concatenation: `.=`. 79}ExprAssignmentType ; 80 81#define EXPR_OPT_SCOPE_LIST \ 82 ((char[]){ kExprOptScopeGlobal, kExprOptScopeLocal }) 83 84/// All possible variable scopes 85typedef enum { 86kExprVarScopeMissing = 0 , 87kExprVarScopeScript = 's' , 88kExprVarScopeGlobal = 'g' , 89kExprVarScopeVim = 'v' , 90kExprVarScopeBuffer = 'b' , 91kExprVarScopeWindow = 'w' , 92kExprVarScopeTabpage = 't' , 93kExprVarScopeLocal = 'l' , 94kExprVarScopeArguments = 'a' , 95}ExprVarScope ; 96 97#define EXPR_VAR_SCOPE_LIST \ 98 ((char[]) { \ 99 kExprVarScopeScript, kExprVarScopeGlobal, kExprVarScopeVim, \ 100 kExprVarScopeBuffer, kExprVarScopeWindow, kExprVarScopeTabpage, \ 101 kExprVarScopeLocal, kExprVarScopeBuffer, kExprVarScopeArguments, \ 102 }) 103 104/// Lexer token 105typedef struct { 106ParserPosition start ; 107size_t len ; 108LexExprTokenType type ; 109union { 110struct { 111ExprComparisonType type ;///< Comparison type. 112ExprCaseCompareStrategy ccs ;///< Case comparison strategy. 113bool inv ;///< True if comparison is to be inverted. 114 }cmp ;///< For kExprLexComparison. 115 116struct { 117enum { 118kExprLexMulMul ,///< Real multiplication. 119kExprLexMulDiv ,///< Division. 120kExprLexMulMod ,///< Modulo. 121 }type ;///< Multiplication type. 122 }mul ;///< For kExprLexMultiplication. 123 124struct { 125bool closing ;///< True if bracket/etc is a closing one. 126 }brc ;///< For brackets/braces/parenthesis. 127 128struct { 129int name ;///< Register name, may be -1 if name not present. 130 }reg ;///< For kExprLexRegister. 131 132struct { 133bool closed ;///< True if quote was closed. 134 }str ;///< For kExprLexSingleQuotedString and kExprLexDoubleQuotedString. 135 136struct { 137const char * name ;///< Option name start. 138size_t len ;///< Option name length. 139ExprOptScope scope ;///< Option scope: &l:, &g: or not specified. 140 }opt ;///< Option properties. 141 142struct { 143ExprVarScope scope ;///< Scope character or 0 if not present. 144bool autoload ;///< Has autoload characters. 145 }var ;///< For kExprLexPlainIdentifier 146 147struct { 148LexExprTokenType type ;///< Suggested type for parsing incorrect code. 149const char * msg ;///< Error message. 150 }err ;///< For kExprLexInvalid 151 152struct { 153union { 154float_T floating ; 155uvarnumber_T integer ; 156 }val ;///< Number value. 157uint8_t base ;///< Base: 2, 8, 10 or 16. 158bool is_float ;///< True if number is a floating-point. 159 }num ;///< For kExprLexNumber 160 161struct { 162ExprAssignmentType type ; 163 }ass ;///< For kExprLexAssignment 164 }data ;///< Additional data, if needed. 165}LexExprToken ; 166 167typedef enum { 168/// If set, “pointer” to the current byte in pstate will not be shifted 169kELFlagPeek = (1 <<0 ), 170/// Determines whether scope is allowed to come before the identifier 171kELFlagForbidScope = (1 <<1 ), 172/// Determines whether floating-point numbers are allowed 173/// 174/// I.e. whether dot is a decimal point separator or is not a part of 175/// a number at all. 176kELFlagAllowFloat = (1 <<2 ), 177/// Determines whether `is` and `isnot` are seen as comparison operators 178/// 179/// If set they are supposed to be just regular identifiers. 180kELFlagIsNotCmp = (1 <<3 ), 181/// Determines whether EOC tokens are allowed 182/// 183/// If set then it will yield Invalid token with E15 in place of EOC one if 184/// “EOC” is something like "|". It is fine with emitting EOC at the end of 185/// string still, with or without this flag set. 186kELFlagForbidEOC = (1 <<4 ), 187// XXX Whenever you add a new flag, alter klee_assume() statement in 188// viml_expressions_lexer.c. 189}LexExprFlags ; 190 191/// Expression AST node type 192typedef enum { 193kExprNodeMissing = 0 , 194kExprNodeOpMissing , 195kExprNodeTernary ,///< Ternary operator. 196kExprNodeTernaryValue ,///< Ternary operator, colon. 197kExprNodeRegister ,///< Register. 198kExprNodeSubscript ,///< Subscript. 199kExprNodeListLiteral ,///< List literal. 200kExprNodeUnaryPlus , 201kExprNodeBinaryPlus , 202kExprNodeNested ,///< Nested parenthesised expression. 203kExprNodeCall ,///< Function call. 204/// Plain identifier: simple variable/function name 205/// 206/// Looks like "string", "g:Foo", etc: consists from a single 207/// kExprLexPlainIdentifier token. 208kExprNodePlainIdentifier , 209/// Plain dictionary key, for use with kExprNodeConcatOrSubscript 210kExprNodePlainKey , 211/// Complex identifier: variable/function name with curly braces 212kExprNodeComplexIdentifier , 213/// Figure brace expression which is not yet known 214/// 215/// May resolve to any of kExprNodeDictLiteral, kExprNodeLambda or 216/// kExprNodeCurlyBracesIdentifier. 217kExprNodeUnknownFigure , 218kExprNodeLambda ,///< Lambda. 219kExprNodeDictLiteral ,///< Dict literal. 220kExprNodeCurlyBracesIdentifier ,///< Part of the curly braces name. 221kExprNodeComma ,///< Comma “operator”. 222kExprNodeColon ,///< Colon “operator”. 223kExprNodeArrow ,///< Arrow “operator”. 224kExprNodeComparison ,///< Various comparison operators. 225/// Concat operator 226/// 227/// To be only used in cases when it is known for sure it is not a subscript. 228kExprNodeConcat , 229/// Concat or subscript operator 230/// 231/// For cases when it is not obvious whether expression is a concat or 232/// a subscript. May only have either number or plain identifier as the second 233/// child. To make it easier to avoid curly braces in place of 234/// kExprNodePlainIdentifier node kExprNodePlainKey is used. 235kExprNodeConcatOrSubscript , 236kExprNodeInteger ,///< Integral number. 237kExprNodeFloat ,///< Floating-point number. 238kExprNodeSingleQuotedString , 239kExprNodeDoubleQuotedString , 240kExprNodeOr , 241kExprNodeAnd , 242kExprNodeUnaryMinus , 243kExprNodeBinaryMinus , 244kExprNodeNot , 245kExprNodeMultiplication , 246kExprNodeDivision , 247kExprNodeMod , 248kExprNodeOption , 249kExprNodeEnvironment , 250kExprNodeAssignment , 251// XXX When modifying this list also modify east_node_type_tab both in parser 252// and in tests, and you most likely will also have to alter list of 253// highlight groups stored in highlight_init_cmdline variable. 254}ExprASTNodeType ; 255 256typedef struct expr_ast_node ExprASTNode ; 257 258/// Structure representing one AST node 259struct expr_ast_node { 260ExprASTNodeType type ;///< Node type. 261/// Node children: e.g. for 1 + 2 nodes 1 and 2 will be children of +. 262ExprASTNode * children ; 263/// Next node: e.g. for 1 + 2 child nodes 1 and 2 are put into a single-linked 264/// list: `(+)->children` references only node 1, node 2 is in 265/// `(+)->children->next`. 266ExprASTNode * next ; 267ParserPosition start ; 268size_t len ; 269union { 270struct { 271int name ;///< Register name, may be -1 if name not present. 272 }reg ;///< For kExprNodeRegister. 273struct { 274/// Which nodes UnknownFigure can’t possibly represent. 275struct { 276/// True if UnknownFigure may actually represent dictionary literal. 277bool allow_dict ; 278/// True if UnknownFigure may actually represent lambda. 279bool allow_lambda ; 280/// True if UnknownFigure may actually be part of curly braces name. 281bool allow_ident ; 282 }type_guesses ; 283/// Highlight chunk index, used for rehighlighting if needed 284size_t opening_hl_idx ; 285 }fig ;///< For kExprNodeUnknownFigure. 286struct { 287ExprVarScope scope ;///< Scope character or 0 if not present. 288/// Actual identifier without scope. 289/// 290/// Points to inside parser reader state. 291const char * ident ; 292size_t ident_len ;///< Actual identifier length. 293 }var ;///< For kExprNodePlainIdentifier and kExprNodePlainKey. 294struct { 295bool got_colon ;///< True if colon was seen. 296 }ter ;///< For kExprNodeTernaryValue. 297struct { 298ExprComparisonType type ;///< Comparison type. 299ExprCaseCompareStrategy ccs ;///< Case comparison strategy. 300bool inv ;///< True if comparison is to be inverted. 301 }cmp ;///< For kExprNodeComparison. 302struct { 303uvarnumber_T value ; 304 }num ;///< For kExprNodeInteger. 305struct { 306float_T value ; 307 }flt ;///< For kExprNodeFloat. 308struct { 309char * value ; 310size_t size ; 311 }str ;///< For kExprNodeSingleQuotedString and 312///< kExprNodeDoubleQuotedString. 313struct { 314const char * ident ;///< Option name start. 315size_t ident_len ;///< Option name length. 316ExprOptScope scope ;///< Option scope: &l:, &g: or not specified. 317 }opt ;///< For kExprNodeOption. 318struct { 319const char * ident ;///< Environment variable name start. 320size_t ident_len ;///< Environment variable name length. 321 }env ;///< For kExprNodeEnvironment. 322struct { 323ExprAssignmentType type ; 324 }ass ;///< For kExprNodeAssignment 325 }data ; 326}; 327 328enum ExprParserFlags { 329/// Allow multiple expressions in a row: e.g. for :echo 330/// 331/// Parser will still parse only one of them though. 332kExprFlagsMulti = (1 <<0 ), 333/// Allow NL, NUL and bar to be EOC 334/// 335/// When parsing expressions input by user bar is assumed to be a binary 336/// operator and other two are spacings. 337kExprFlagsDisallowEOC = (1 <<1 ), 338/// Parse :let argument 339/// 340/// That mean that top level node must be an assignment and first nodes 341/// belong to lvalues. 342kExprFlagsParseLet = (1 <<2 ), 343// XXX whenever you add a new flag, alter klee_assume() statement in 344// viml_expressions_parser.c, nvim_parse_expression() flags parsing 345// alongside with its documentation and flag sets in check_parsing() 346// function in expressions parser functional and unit tests. 347}; 348 349/// AST error definition 350typedef struct { 351/// Error message. Must contain a single printf format atom: %.*s. 352const char * msg ; 353/// Error message argument: points to the location of the error. 354const char * arg ; 355/// Message argument length: length till the end of string. 356int arg_len ; 357}ExprASTError ; 358 359/// Structure representing complete AST for one expression 360typedef struct { 361/// When AST is not correct this message will be printed. 362/// 363/// Uses `semsg(msg, arg_len, arg);`, `msg` is assumed to contain only `%.*s`. 364ExprASTError err ; 365/// Root node of the AST. 366ExprASTNode * root ; 367}ExprAST ; 368 369/// Array mapping ExprASTNodeType to maximum amount of children node may have 370extern const uint8_t node_maxchildren []; 371 372/// Array mapping ExprASTNodeType values to their stringified versions 373extern const char * const east_node_type_tab []; 374 375/// Array mapping ExprComparisonType values to their stringified versions 376extern const char * const eltkn_cmp_type_tab []; 377 378/// Array mapping ExprCaseCompareStrategy values to their stringified versions 379extern const char * const ccs_tab []; 380 381/// Array mapping ExprAssignmentType values to their stringified versions 382extern const char * const expr_asgn_type_tab []; 383 384#include "viml/parser/expressions.h.generated.h"