-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreesitter.nim
1537 lines (1328 loc) · 60.4 KB
/
treesitter.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{.experimental: "codeReordering".}
import strformat
import strutils, os
import tables
import sets
import json
import osproc
import hashes
import sequtils
import macros
import streams
import algorithm
import compiler/[ast, idents, modulegraphs, options]
import strformat
import tables
import sets
import algorithm
import sequtils
import strutils
type
Vertex* = ref object
indegree: int
name: string
edge: HashSet[string]
proc topoSort*(gl: var OrderedTable[string, Vertex]):seq[string] =
var zeroIndegree: seq[string]
for f,vertex in gl.mpairs:
var indegree = vertex.indegree
# echo fmt"{f} with indegree {indegree}"
if indegree == 0:
zeroIndegree.add f
while zeroIndegree.len > 0:
var z = zeroIndegree.pop
result.add z
for e in gl[z].edge:
gl[e].indegree.dec
for f,vertex in gl.mpairs:
if f notin result and f notin zeroIndegree and vertex.indegree == 0:
zeroIndegree.add f
proc addEdge*(gl: OrderedTable,k:string,v:string) =
if not gl[k].edge.contains v:
gl[k].edge.incl v
gl[v].inDegree.inc
proc initVertex*(gl:var OrderedTable, k:varargs[string]) =
for i in k:
gl[i] = Vertex(name: i)
macro defineEnum(typ: untyped): untyped =
result = newNimNode(nnkStmtList)
# Enum mapped to distinct cint
result.add quote do:
type `typ`* = distinct cint
for i in ["+", "-", "*", "div", "mod", "shl", "shr", "or", "and", "xor", "<", "<=", "==", ">", ">="]:
let
ni = newIdentNode(i)
typout = if i[0] in "<=>": newIdentNode("bool") else: typ # comparisons return bool
if i[0] == '>': # cannot borrow `>` and `>=` from templates
let
nopp = if i.len == 2: newIdentNode("<=") else: newIdentNode("<")
result.add quote do:
proc `ni`*(x: `typ`, y: cint): `typout` = `nopp`(y, x)
proc `ni`*(x: cint, y: `typ`): `typout` = `nopp`(y, x)
proc `ni`*(x, y: `typ`): `typout` = `nopp`(y, x)
else:
result.add quote do:
proc `ni`*(x: `typ`, y: cint): `typout` {.borrow.}
proc `ni`*(x: cint, y: `typ`): `typout` {.borrow.}
proc `ni`*(x, y: `typ`): `typout` {.borrow.}
result.add quote do:
proc `ni`*(x: `typ`, y: int): `typout` = `ni`(x, y.cint)
proc `ni`*(x: int, y: `typ`): `typout` = `ni`(x.cint, y)
let
divop = newIdentNode("/") # `/`()
dlrop = newIdentNode("$") # `$`()
notop = newIdentNode("not") # `not`()
result.add quote do:
proc `divop`*(x, y: `typ`): `typ` = `typ`((x.float / y.float).cint)
proc `divop`*(x: `typ`, y: cint): `typ` = `divop`(x, `typ`(y))
proc `divop`*(x: cint, y: `typ`): `typ` = `divop`(`typ`(x), y)
proc `divop`*(x: `typ`, y: int): `typ` = `divop`(x, y.cint)
proc `divop`*(x: int, y: `typ`): `typ` = `divop`(x.cint, y)
proc `dlrop`*(x: `typ`): string {.borrow.}
proc `notop`*(x: `typ`): `typ` {.borrow.}
defineEnum(TSInputEncoding)
defineEnum(TSSymbolType)
defineEnum(TSLogType)
defineEnum(TSQueryPredicateStepType)
defineEnum(TSQueryError)
const
TREE_SITTER_LANGUAGE_VERSION* = 11
TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION* = 9
TSInputEncodingUTF8* = (0).TSInputEncoding
TSInputEncodingUTF16* = (TSInputEncodingUTF8 + 1).TSInputEncoding
TSSymbolTypeRegular* = (0).TSSymbolType
TSSymbolTypeAnonymous* = (TSSymbolTypeRegular + 1).TSSymbolType
TSSymbolTypeAuxiliary* = (TSSymbolTypeAnonymous + 1).TSSymbolType
TSLogTypeParse* = (0).TSLogType
TSLogTypeLex* = (TSLogTypeParse + 1).TSLogType
TSQueryPredicateStepTypeDone* = (0).TSQueryPredicateStepType
TSQueryPredicateStepTypeCapture* = (TSQueryPredicateStepTypeDone + 1).TSQueryPredicateStepType
TSQueryPredicateStepTypeString* = (TSQueryPredicateStepTypeCapture + 1).TSQueryPredicateStepType
TSQueryErrorNone* = (0).TSQueryError
TSQueryErrorSyntax* = (TSQueryErrorNone + 1).TSQueryError
TSQueryErrorNodeType* = (TSQueryErrorSyntax + 1).TSQueryError
TSQueryErrorField* = (TSQueryErrorNodeType + 1).TSQueryError
TSQueryErrorCapture* = (TSQueryErrorField + 1).TSQueryError
const
gAtoms* {.used.} = @[
"field_identifier",
"identifier",
"number_literal",
"char_literal",
"preproc_arg",
"primitive_type",
"sized_type_specifier",
"type_identifier"
].toHashSet()
gExpressions* {.used.} = @[
"parenthesized_expression",
"bitwise_expression",
"shift_expression",
"math_expression",
"escape_sequence",
"binary_expression",
"unary_expression"
].toHashSet()
gEnumVals* {.used.} = @[
"identifier",
"number_literal",
"char_literal"
].concat(toSeq(gExpressions.items))
type
Status* = enum
success, unknown, error
type
Feature* = enum
ast2
Symbol* = object
name*: string
parent*: string
kind*: NimSymKind
override*: string
StringHash = HashSet[string]
OnSymbol* = proc(sym: var Symbol) {.cdecl.}
OnSymbolOverrideFinal* = proc(typ: string): StringHash {.cdecl.}
State* = ref object
# Command line arguments to toast - some forwarded from cimport.nim
compile*: seq[string] # `--compile` to create `{.compile.}` entries in generated wrapper
convention*: string # `--convention | -C` to change calling convention from cdecl default
debug*: bool # `cDebug()` or `--debug | -d` to enable debug mode
defines*: seq[string] # Symbols added by `cDefine()` and `--define | -D` for C/C++ preprocessor/compiler
dynlib*: string # `cImport(dynlib)` or `--dynlib | -l` to specify variable containing library name
exclude*: seq[string] # files or directories to exclude from the wrapped output
feature*: seq[Feature] # `--feature | -f` feature flags enabled
includeDirs*: seq[string] # Paths added by `cIncludeDir()` and `--includeDirs | -I` for C/C++ preprocessor/compiler
mode*: string # `cImport(mode)` or `--mode | -m` to override detected compiler mode - c or cpp
nim*: string # `--nim` to specify full path to Nim compiler
noComments*: bool # `--noComments | -c` to disable rendering comments in wrappers
noHeader*: bool # `--noHeader | -H` to skip {.header.} pragma in wrapper
passC*: seq[string] # `--passC` to create `{.passC.}` entries in the generated wrapper
passL*: seq[string] # `--passL` to create `{.passL.}` entries in the generated wrapper
past*: bool # `--past | -a` to print tree-sitter AST of code
pluginSourcePath*: string # `--pluginSourcePath` specified path to plugin file to compile and load
pnim*: bool # `--pnim | -n` to render Nim wrapper for header
preprocess*: bool # `--preprocess | -p` to enable preprocessing of code before wrapping
prefix*: seq[string] # `--prefix` strings to strip from start of identifiers
recurse*: bool # `--recurse | -r` to recurse into #include files in headers specified
replace*: OrderedTableRef[string, string]
# `--replace | -G` replacement rules for identifiers
suffix*: seq[string] # `--suffix` strings to strip from end of identifiers
symOverride*: seq[string] # `cSkipSymbol()`, `cOverride()` and `--symOverride | -O` symbols to skip during wrapping
typeMap*: TableRef[string, string]
# `--typeMap | -T` to map instances of type X to Y - e.g. ABC=cint
# Data fields
code*: string # Contents of header file currently being processed
currentHeader*: string # Const name of header being currently processed
impShort*: string # Short base name for pragma in output
outputHandle*: File # `--output | -o` open file handle
sourceFile*: string # Full path of header being currently processed
functionDef*: string
# Plugin callbacks
onSymbol*, onSymbolOverride*: OnSymbol
onSymbolOverrideFinal*: OnSymbolOverrideFinal
# Symbol tables
constIdentifiers*: HashSet[string] # Const names for enum casting
identifiers*: TableRef[string, string] # Symbols that have been declared so far indexed by nimName
skippedSyms*: HashSet[string] # Symbols that have been skipped due to being unwrappable or
# the user provided override is blank
headersProcessed*: HashSet[string] # Headers already processed directly or recursively
# Nim compiler objects
constSection*, enumSection*, pragmaSection*, procSection*, typeSection*, varSection*: PNode
identCache*: IdentCache
config*: ConfigRef
graph*: ModuleGraph
# Table of symbols to generated AST PNode - used to implement forward declarations
identifierNodes*: TableRef[string, PNode]
# Used for the exprparser.nim module
currentExpr*, currentTyCastName*: string
# Controls whether or not the current expression
# should validate idents against currently defined idents
skipIdentValidation*: bool
# Top level header for wrapper output - include imported types, pragmas and other info
wrapperHeader*: string
# cimport.nim specific
compcache*: seq[string] # `cCompile()` list of files already processed
nocache*: bool # `cDisableCaching()` to disable caching of artifacts
overrides*: string # `cOverride()` code which gets added to `cPlugin()` output
pluginSource*: string # `cPlugin()` generated code to write to plugin file from
searchDirs*: seq[string] # `cSearchPath()` added directories for header search
Config* = ref object
NimMajor*: int
NimMinor*: int
NimPatch*: int
paths*: OrderedSet[string]
nimblePaths*: OrderedSet[string]
nimcacheDir*: string
outDir*: string
MultipleValueSetting* {.pure.} = enum ## \
## settings resulting in a seq of string values
nimblePaths, ## the nimble path(s)
searchPaths, ## the search path for modules
lazyPaths, ## experimental: even more paths
commandArgs, ## the arguments passed to the Nim compiler
cincludes, ## the #include paths passed to the C/C++ compiler
clibs
SingleValueSetting* {.pure.} = enum ## \
## settings resulting in a single string value
arguments, ## experimental: the arguments passed after '-r'
outFile, ## experimental: the output file
outDir, ## the output directory
nimcacheDir, ## the location of the 'nimcache' directory
projectName, ## the project's name that is being compiled
projectPath, ## experimental: some path to the project that is being compiled
projectFull, ## the full path to the project that is being compiled
command, ## experimental: the command (e.g. 'c', 'cpp', 'doc') passed to
## the Nim compiler
commandLine, ## experimental: the command line passed to Nim
linkOptions, ## additional options passed to the linker
compileOptions, ## additional options passed to the C/C++ compiler
ccompilerPath ## the path to the C/C++ compiler
backend ## the backend (eg: c|cpp|objc|js); both `nim doc --backend:js`
## and `nim js` would imply backend=js
when nimvm:
var
gStateCT* {.compileTime, used.} = new(State)
else:
var
gState* = State(mode:"c",includeDirs: @["/mnt/c/openssl","/mnt/c/openssl/ssl","/mnt/c/openssl/include","/mnt/c/nghttp3/lib/includes","/mnt/c/nghttp3/lib","/mnt/c/ngtcp2/crypto/includes",
"/mnt/c/ngtcp2/crypto","/mnt/c/ngtcp2/lib","/mnt/c/ngtcp2/lib/includes","/mnt/c/ngtcp2/third-party","/mnt/c/ngtcp2/third-party/http-parser"])
proc querySettingSeq*(setting: MultipleValueSetting): seq[string] {.
compileTime, noSideEffect.} = discard
proc querySetting*(setting: SingleValueSetting): string {.
compileTime, noSideEffect.} = discard
proc stripName(path, projectName: string): string =
# Remove `pname_d|r` tail from path
let
(head, tail) = path.splitPath()
if projectName in tail:
result = head
else:
result = path
proc getProjectDir*(): string =
## Get project directory for this compilation - returns `""` at runtime
when nimvm:
when (NimMajor, NimMinor, NimPatch) >= (1, 2, 0):
# If nim v1.2.0+, get from `std/compilesettings`
result = querySetting(projectFull).parentDir()
else:
# Get from `macros`
result = getProjectPath()
else:
discard
proc getCurrentNimCompiler*(): string =
when nimvm:
result = getCurrentCompilerExe()
when defined(nimsuggest):
result = result.replace("nimsuggest", "nim")
else:
result = gState.nim
proc sanitizePath*(path: string, noQuote = false, sep = $DirSep): string =
result = path.multiReplace([("\\\\", sep), ("\\", sep), ("/", sep)])
if not noQuote:
result = result.quoteShell
proc fixCmd*(cmd: string): string =
when defined(Windows):
# Replace 'cd d:\abc' with 'd: && cd d:\abc`
var filteredCmd = cmd
if cmd.toLower().startsWith("cd"):
var
colonIndex = cmd.find(":")
driveLetter = cmd.substr(colonIndex-1, colonIndex)
if (driveLetter[0].isAlphaAscii() and
driveLetter[1] == ':' and
colonIndex == 4):
filteredCmd = &"{driveLetter} && {cmd}"
result = "cmd /c " & filteredCmd
elif defined(posix):
result = cmd
else:
doAssert false
proc getJson(projectDir: string): JsonNode =
# Get `nim dump` json value for `projectDir`
var
cmd = &"{getCurrentNimCompiler()} --hints:off --dump.format:json dump dummy"
dump = ""
ret = 0
if projectDir.len != 0:
# Run `nim dump` in `projectDir` if specified
cmd = &"cd {projectDir.sanitizePath} && " & cmd
cmd = fixCmd(cmd)
when nimvm:
(dump, ret) = gorgeEx(cmd)
else:
(dump, ret) = execCmdEx(cmd)
try:
result = parseJson(dump)
except JsonParsingError as e:
echo "# Failed to parse `nim dump` output: " & e.msg
proc jsonToSeq(node: JsonNode, key: string): seq[string] =
# Convert JsonArray to seq[string] for specified `key`
if node.hasKey(key):
for elem in node[key].getElems():
result.add elem.getStr()
proc getOsCacheDir(): string =
# OS default cache directory
when defined(posix):
result = getEnv("XDG_CACHE_HOME", getHomeDir() / ".cache") / "nim"
else:
result = getHomeDir() / "nimcache"
proc getAbsoluteDir(projectDir, path: string): string =
# Path is relative to `projectDir` if not absolute
if path.isAbsolute():
result = path
else:
result = (projectDir / path).normalizedPath()
proc getNimConfig*(projectDir = ""): Config =
# Get `paths` - list of paths to be forwarded to Nim
result = new(Config)
var
libPath, version: string
lazyPaths, searchPaths: seq[string]
when nimvm:
result.NimMajor = NimMajor
result.NimMinor = NimMinor
result.NimPatch = NimPatch
when (NimMajor, NimMinor, NimPatch) >= (1, 2, 0):
# Get value at compile time from `std/compilesettings`
libPath = getCurrentCompilerExe().parentDir().parentDir() / "lib"
lazyPaths = querySettingSeq(MultipleValueSetting.lazyPaths)
searchPaths = querySettingSeq(MultipleValueSetting.searchPaths)
result.nimcacheDir = stripName(
querySetting(SingleValueSetting.nimcacheDir),
querySetting(SingleValueSetting.projectName)
)
result.outDir = querySetting(SingleValueSetting.outDir)
else:
discard
let
# Get project directory for < v1.2.0 at compile time
projectDir = if projectDir.len != 0: projectDir else: getProjectDir()
# Not Nim v1.2.0+ or runtime
if libPath.len == 0:
let
dumpJson = getJson(projectDir)
if dumpJson != nil:
if dumpJson.hasKey("version"):
version = dumpJson["version"].getStr()
lazyPaths = jsonToSeq(dumpJson, "lazyPaths")
searchPaths = jsonToSeq(dumpJson, "lib_paths")
if dumpJson.hasKey("libpath"):
libPath = dumpJson["libpath"].getStr()
elif searchPaths.len != 0:
# Usually `libPath` is last entry in `searchPaths`
libPath = searchPaths[^1]
if dumpJson.hasKey("nimcache"):
result.nimcacheDir = stripName(dumpJson["nimcache"].getStr(), "dummy")
if dumpJson.hasKey("outdir"):
result.outDir = dumpJson["outdir"].getStr()
# Parse version
if version.len != 0:
let
splversion = version.split({'.'}, maxsplit = 3)
result.NimMajor = splversion[0].parseInt()
result.NimMinor = splversion[1].parseInt()
result.NimPatch = splversion[2].parseInt()
# Find non standard lib paths added to `searchPath`
for path in searchPaths:
let
path = getAbsoluteDir(projectDir, path)
if libPath notin path:
result.paths.incl path
# Find `nimblePaths` in `lazyPaths`
for path in lazyPaths:
let
path = getAbsoluteDir(projectDir, path)
(_, tail) = path.strip(leading = false, chars = {'/', '\\'}).splitPath()
if tail == "pkgs":
# Nimble path probably
result.nimblePaths.incl path
# Find `paths` in `lazyPaths` that aren't within `nimblePaths`
# Have to do this separately since `nimblePaths` could be after
# packages in `lazyPaths`
for path in lazyPaths:
let
path = getAbsoluteDir(projectDir, path)
var skip = false
for npath in result.nimblePaths:
if npath in path:
skip = true
break
if not skip:
result.paths.incl path
if result.nimcacheDir.len == 0:
result.nimcacheDir = getOsCacheDir()
if result.outDir.len == 0:
result.outDir = projectDir
proc getNimcacheDir*(projectDir = ""): string =
## Get nimcache directory for current compilation or specified `projectDir`
let
cfg = getNimConfig(projectDir)
result = cfg.nimcacheDir
proc getNimteropCacheDir*(): string =
result = getNimcacheDir() / "nimterop"
proc execAction*(cmd: string, retry = 0, die = true, cache = false,
cacheKey = "", onRetry: proc() = nil,
onError: proc(output: string, err: int) = nil): tuple[output: string, ret: int] =
let
ccmd = fixCmd(cmd)
when nimvm:
# Cache results for speedup if cache = true
# Else cache for preserving functionality in nimsuggest and nimcheck
let
hash = (ccmd & cacheKey).hash().abs()
cachePath = getNimteropCacheDir() / "execCache" / "nimterop_" & $hash
cacheFile = cachePath & ".txt"
retFile = cachePath & "_ret.txt"
when defined(nimsuggest) or defined(nimcheck):
# Load results from cache file if generated in previous run
if fileExists(cacheFile) and fileExists(retFile):
result.output = cacheFile.readFile()
result.ret = retFile.readFile().parseInt()
elif die:
doAssert false, "Results not cached - run nim c/cpp at least once\n" & ccmd
else:
if cache and fileExists(cacheFile) and fileExists(retFile) and not compileOption("forceBuild"):
# Return from cache when requested
result.output = cacheFile.readFile()
result.ret = retFile.readFile().parseInt()
else:
# Execute command and store results in cache
(result.output, result.ret) = gorgeEx(ccmd)
if result.ret == 0 or die == false:
# mkdir for execCache dir (circular dependency)
let dir = cacheFile.parentDir()
if not dirExists(dir):
let flag = when not defined(Windows): "-p" else: ""
discard execAction(&"mkdir {flag} {dir.sanitizePath}")
cacheFile.writeFile(result.output)
retFile.writeFile($result.ret)
else:
# Used by toast
(result.output, result.ret) = execCmdEx(ccmd)
# On failure, retry or die as requested
if result.ret != 0:
if retry > 0:
if not onRetry.isNil:
onRetry()
sleep(500)
result = execAction(cmd, retry = retry - 1, die, cache, cacheKey)
else:
if not onError.isNil:
onError(result.output, result.ret)
doAssert not die, "Command failed: " & $result.ret & "\ncmd: " & ccmd &
"\nresult:\n" & result.output
proc rmFile*(source: string, dir = false) =
## Remove a file or pattern at compile time
let
source = source.replace("/", $DirSep)
cmd =
when defined(Windows):
if dir:
"rd /s/q"
else:
"del /q/f"
else:
"rm -rf"
exists =
if dir:
dirExists(source)
else:
fileExists(source)
if exists:
discard execAction(&"{cmd} {source.sanitizePath}", retry = 2)
proc rmDir*(dir: string) =
## Remove a directory or pattern at compile time
rmFile(dir, dir = true)
proc getProjectCacheDir*(name: string, forceClean = true): string =
result = getNimteropCacheDir() / name
if forceClean and compileOption("forceBuild"):
echo "# Removing " & result
rmDir(result)
const
cacheDir* = getProjectCacheDir("nimterop", forceClean = false)
const sourcePath = cacheDir / "treesitter" / "lib"
when defined(Linux) and defined(gcc):
{.passC: "-std=c11".}
{.passC: "-I$1" % (sourcePath / "include").}
{.passC: "-I$1" % (sourcePath / "src").}
{.compile: sourcePath / "src" / "lib.c".}
{.push hint[ConvFromXtoItselfNotNeeded]: off.}
{.pragma: impapiHdr, header: sourcePath / "include" / "tree_sitter" / "api.h".}
{.pragma: imptreeHdr, header: sourcePath / "include" / "tree_sitter" / "tree.h".}
type
TSSymbol* {.importc, impapiHdr.} = uint16
TSFieldId* {.importc, impapiHdr.} = uint16
TSLanguage* {.importc, impapiHdr, incompleteStruct.} = object
TSParser* {.importc, impapiHdr, incompleteStruct.} = object
TSTree* {.importc, impapiHdr, incompleteStruct.} = object
TSQuery* {.importc, impapiHdr, incompleteStruct.} = object
TSQueryCursor* {.importc, impapiHdr, incompleteStruct.} = object
TSPoint* {.bycopy, importc, impapiHdr.} = object
row*: uint32
column*: uint32
TSRange* {.bycopy, importc, impapiHdr.} = object
start_point*: TSPoint
end_point*: TSPoint
start_byte*: uint32
end_byte*: uint32
TSInput* {.bycopy, importc, impapiHdr.} = object
payload*: pointer
read*: proc (payload: pointer; byte_index: uint32; position: TSPoint;
bytes_read: ptr uint32): cstring {.cdecl.}
encoding*: TSInputEncoding
TSLogger* {.bycopy, importc, impapiHdr.} = object
payload*: pointer
log*: proc (payload: pointer; a2: TSLogType; a3: cstring) {.cdecl.}
TSInputEdit* {.bycopy, importc, impapiHdr.} = object
start_byte*: uint32
old_end_byte*: uint32
new_end_byte*: uint32
start_point*: TSPoint
old_end_point*: TSPoint
new_end_point*: TSPoint
TSNode* {.bycopy, importc, impapiHdr.} = object
context*: array[4, uint32]
id*: pointer
tree*: ptr TSTree
TSTreeCursor* {.bycopy, importc, impapiHdr.} = object
tree*: pointer
id*: pointer
context*: array[2, uint32]
TSQueryCapture* {.bycopy, importc, impapiHdr.} = object
node*: TSNode
index*: uint32
TSQueryMatch* {.bycopy, importc, impapiHdr.} = object
id*: uint32
pattern_index*: uint16
capture_count*: uint16
captures*: ptr TSQueryCapture
TSQueryPredicateStep* {.bycopy, importc, impapiHdr.} = object
`type`*: TSQueryPredicateStepType
value_id*: uint32
proc ts_parser_new*(): ptr TSParser {.importc, cdecl, impapiHdr.}
proc ts_parser_delete*(parser: ptr TSParser) {.importc, cdecl, impapiHdr.}
proc ts_parser_set_language*(self: ptr TSParser; language: ptr TSLanguage): bool {.
importc, cdecl, impapiHdr.}
proc ts_parser_language*(self: ptr TSParser): ptr TSLanguage {.importc, cdecl, impapiHdr.}
proc ts_parser_set_included_ranges*(self: ptr TSParser; ranges: ptr TSRange;
length: uint32) {.importc, cdecl, impapiHdr.}
proc ts_parser_included_ranges*(self: ptr TSParser; length: ptr uint32): ptr TSRange {.
importc, cdecl, impapiHdr.}
proc ts_parser_parse*(self: ptr TSParser; old_tree: ptr TSTree; input: TSInput): ptr TSTree {.
importc, cdecl, impapiHdr.}
proc ts_parser_parse_string*(self: ptr TSParser; old_tree: ptr TSTree; string: cstring;
length: uint32): ptr TSTree {.importc, cdecl, impapiHdr.}
proc ts_parser_parse_string_encoding*(self: ptr TSParser; old_tree: ptr TSTree;
string: cstring; length: uint32;
encoding: TSInputEncoding): ptr TSTree {.
importc, cdecl, impapiHdr.}
proc ts_parser_reset*(self: ptr TSParser) {.importc, cdecl, impapiHdr.}
proc ts_parser_set_timeout_micros*(self: ptr TSParser; timeout: uint64) {.importc,
cdecl, impapiHdr.}
proc ts_parser_timeout_micros*(self: ptr TSParser): uint64 {.importc, cdecl, impapiHdr.}
proc ts_parser_set_cancellation_flag*(self: ptr TSParser; flag: ptr uint) {.importc,
cdecl, impapiHdr.}
proc ts_parser_cancellation_flag*(self: ptr TSParser): ptr uint {.importc, cdecl,
impapiHdr.}
proc ts_parser_set_logger*(self: ptr TSParser; logger: TSLogger) {.importc, cdecl,
impapiHdr.}
proc ts_parser_logger*(self: ptr TSParser): TSLogger {.importc, cdecl, impapiHdr.}
proc ts_parser_print_dot_graphs*(self: ptr TSParser; file: cint) {.importc, cdecl,
impapiHdr.}
proc ts_parser_halt_on_error*(self: ptr TSParser; halt: bool) {.importc, cdecl,
impapiHdr.}
proc ts_tree_copy*(self: ptr TSTree): ptr TSTree {.importc, cdecl, impapiHdr.}
proc ts_tree_delete*(self: ptr TSTree) {.importc, cdecl, impapiHdr.}
proc ts_tree_root_node*(self: ptr TSTree): TSNode {.importc, cdecl, impapiHdr.}
proc ts_tree_language*(a1: ptr TSTree): ptr TSLanguage {.importc, cdecl, impapiHdr.}
proc ts_tree_edit*(self: ptr TSTree; edit: ptr TSInputEdit) {.importc, cdecl, impapiHdr.}
proc ts_tree_get_changed_ranges*(old_tree: ptr TSTree; new_tree: ptr TSTree;
length: ptr uint32): ptr TSRange {.importc, cdecl,
impapiHdr.}
proc ts_tree_print_dot_graph*(a1: ptr TSTree; a2: File) {.importc, cdecl, impapiHdr.}
proc ts_node_type*(a1: TSNode): cstring {.importc, cdecl, impapiHdr.}
proc ts_node_symbol*(a1: TSNode): TSSymbol {.importc, cdecl, impapiHdr.}
proc ts_node_start_byte*(a1: TSNode): uint32 {.importc, cdecl, impapiHdr.}
proc ts_node_start_point*(a1: TSNode): TSPoint {.importc, cdecl, impapiHdr.}
proc ts_node_end_byte*(a1: TSNode): uint32 {.importc, cdecl, impapiHdr.}
proc ts_node_end_point*(a1: TSNode): TSPoint {.importc, cdecl, impapiHdr.}
proc ts_node_string*(a1: TSNode): cstring {.importc, cdecl, impapiHdr.}
proc ts_node_is_null*(a1: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_node_is_named*(a1: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_node_is_missing*(a1: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_node_is_extra*(a1: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_node_has_changes*(a1: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_node_has_error*(a1: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_node_parent*(a1: TSNode): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_child*(a1: TSNode; a2: uint32): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_child_count*(a1: TSNode): uint32 {.importc, cdecl, impapiHdr.}
proc ts_node_named_child*(a1: TSNode; a2: uint32): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_named_child_count*(a1: TSNode): uint32 {.importc, cdecl, impapiHdr.}
proc ts_node_child_by_field_name*(self: TSNode; field_name: cstring;
field_name_length: uint32): TSNode {.importc,
cdecl, impapiHdr.}
proc ts_node_child_by_field_id*(a1: TSNode; a2: TSFieldId): TSNode {.importc, cdecl,
impapiHdr.}
proc ts_node_next_sibling*(a1: TSNode): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_prev_sibling*(a1: TSNode): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_next_named_sibling*(a1: TSNode): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_prev_named_sibling*(a1: TSNode): TSNode {.importc, cdecl, impapiHdr.}
proc ts_node_first_child_for_byte*(a1: TSNode; a2: uint32): TSNode {.importc, cdecl,
impapiHdr.}
proc ts_node_first_named_child_for_byte*(a1: TSNode; a2: uint32): TSNode {.importc,
cdecl, impapiHdr.}
proc ts_node_descendant_for_byte_range*(a1: TSNode; a2: uint32; a3: uint32): TSNode {.
importc, cdecl, impapiHdr.}
proc ts_node_descendant_for_point_range*(a1: TSNode; a2: TSPoint; a3: TSPoint): TSNode {.
importc, cdecl, impapiHdr.}
proc ts_node_named_descendant_for_byte_range*(a1: TSNode; a2: uint32; a3: uint32): TSNode {.
importc, cdecl, impapiHdr.}
proc ts_node_named_descendant_for_point_range*(a1: TSNode; a2: TSPoint; a3: TSPoint): TSNode {.
importc, cdecl, impapiHdr.}
proc ts_node_edit*(a1: ptr TSNode; a2: ptr TSInputEdit) {.importc, cdecl, impapiHdr.}
proc ts_node_eq*(a1: TSNode; a2: TSNode): bool {.importc, cdecl, impapiHdr.}
proc ts_tree_cursor_new*(a1: TSNode): TSTreeCursor {.importc, cdecl, impapiHdr.}
proc ts_tree_cursor_delete*(a1: ptr TSTreeCursor) {.importc, cdecl, impapiHdr.}
proc ts_tree_cursor_reset*(a1: ptr TSTreeCursor; a2: TSNode) {.importc, cdecl, impapiHdr.}
proc ts_tree_cursor_current_node*(a1: ptr TSTreeCursor): TSNode {.importc, cdecl,
impapiHdr.}
proc ts_tree_cursor_current_field_name*(a1: ptr TSTreeCursor): cstring {.importc,
cdecl, impapiHdr.}
proc ts_tree_cursor_current_field_id*(a1: ptr TSTreeCursor): TSFieldId {.importc,
cdecl, impapiHdr.}
proc ts_tree_cursor_goto_parent*(a1: ptr TSTreeCursor): bool {.importc, cdecl,
impapiHdr.}
proc ts_tree_cursor_goto_next_sibling*(a1: ptr TSTreeCursor): bool {.importc, cdecl,
impapiHdr.}
proc ts_tree_cursor_goto_first_child*(a1: ptr TSTreeCursor): bool {.importc, cdecl,
impapiHdr.}
proc ts_tree_cursor_goto_first_child_for_byte*(a1: ptr TSTreeCursor; a2: uint32): int64 {.
importc, cdecl, impapiHdr.}
proc ts_tree_cursor_copy*(a1: ptr TSTreeCursor): TSTreeCursor {.importc, cdecl,
impapiHdr.}
proc ts_query_new*(language: ptr TSLanguage; source: cstring; source_len: uint32;
error_offset: ptr uint32; error_type: ptr TSQueryError): ptr TSQuery {.
importc, cdecl, impapiHdr.}
proc ts_query_delete*(a1: ptr TSQuery) {.importc, cdecl, impapiHdr.}
proc ts_query_pattern_count*(a1: ptr TSQuery): uint32 {.importc, cdecl, impapiHdr.}
proc ts_query_capture_count*(a1: ptr TSQuery): uint32 {.importc, cdecl, impapiHdr.}
proc ts_query_string_count*(a1: ptr TSQuery): uint32 {.importc, cdecl, impapiHdr.}
proc ts_query_start_byte_for_pattern*(a1: ptr TSQuery; a2: uint32): uint32 {.importc,
cdecl, impapiHdr.}
proc ts_query_predicates_for_pattern*(self: ptr TSQuery; pattern_index: uint32;
length: ptr uint32): ptr TSQueryPredicateStep {.
importc, cdecl, impapiHdr.}
proc ts_query_capture_name_for_id*(a1: ptr TSQuery; id: uint32; length: ptr uint32): cstring {.
importc, cdecl, impapiHdr.}
proc ts_query_string_value_for_id*(a1: ptr TSQuery; id: uint32; length: ptr uint32): cstring {.
importc, cdecl, impapiHdr.}
proc ts_query_disable_capture*(a1: ptr TSQuery; a2: cstring; a3: uint32) {.importc,
cdecl, impapiHdr.}
proc ts_query_cursor_new*(): ptr TSQueryCursor {.importc, cdecl, impapiHdr.}
proc ts_query_cursor_delete*(a1: ptr TSQueryCursor) {.importc, cdecl, impapiHdr.}
proc ts_query_cursor_exec*(a1: ptr TSQueryCursor; a2: ptr TSQuery; a3: TSNode) {.importc,
cdecl, impapiHdr.}
proc ts_query_cursor_set_byte_range*(a1: ptr TSQueryCursor; a2: uint32; a3: uint32) {.
importc, cdecl, impapiHdr.}
proc ts_query_cursor_set_point_range*(a1: ptr TSQueryCursor; a2: TSPoint; a3: TSPoint) {.
importc, cdecl, impapiHdr.}
proc ts_query_cursor_next_match*(a1: ptr TSQueryCursor; match: ptr TSQueryMatch): bool {.
importc, cdecl, impapiHdr.}
proc ts_query_cursor_remove_match*(a1: ptr TSQueryCursor; id: uint32) {.importc, cdecl,
impapiHdr.}
proc ts_query_cursor_next_capture*(a1: ptr TSQueryCursor; match: ptr TSQueryMatch;
capture_index: ptr uint32): bool {.importc, cdecl,
impapiHdr.}
proc ts_language_symbol_count*(a1: ptr TSLanguage): uint32 {.importc, cdecl, impapiHdr.}
proc ts_language_symbol_name*(a1: ptr TSLanguage; a2: TSSymbol): cstring {.importc,
cdecl, impapiHdr.}
proc ts_language_symbol_for_name*(self: ptr TSLanguage; string: cstring;
length: uint32; is_named: bool): TSSymbol {.importc,
cdecl, impapiHdr.}
proc ts_language_field_count*(a1: ptr TSLanguage): uint32 {.importc, cdecl, impapiHdr.}
proc ts_language_field_name_for_id*(a1: ptr TSLanguage; a2: TSFieldId): cstring {.
importc, cdecl, impapiHdr.}
proc ts_language_field_id_for_name*(a1: ptr TSLanguage; a2: cstring; a3: uint32): TSFieldId {.
importc, cdecl, impapiHdr.}
proc ts_language_symbol_type*(a1: ptr TSLanguage; a2: TSSymbol): TSSymbolType {.
importc, cdecl, impapiHdr.}
proc ts_language_version*(a1: ptr TSLanguage): uint32 {.importc, cdecl, impapiHdr.}
# proc ts_tree_get_cached_parent*(t: ptr TSTree , n:TSNode ):TSNode {.importc, cdecl, imptreeHdr.}
{.pop.}
proc nimteropRoot*(): string =
currentSourcePath.parentDir.parentDir
proc nimteropSrcDir*(): string =
nimteropRoot() / "nimterop"
proc toastExePath*(): string =
nimteropSrcDir() / ("toast".addFileExt ExeExt)
proc testsIncludeDir*(): string =
nimteropRoot() / "tests" / "include"
# static:
# treesitterSetup()
proc isNil*(node: TSNode): bool =
node.tsNodeIsNull()
proc len*(node: TSNode): int =
if not node.tsNodeIsNull:
result = node.tsNodeNamedChildCount().int
proc `[]`*(node: TSNode, i: SomeInteger): TSNode =
if i < type(i)(node.len()):
result = node.tsNodeNamedChild(i.uint32)
const csrcDir = cacheDir / "treesitter_c" / "src"
{.passC: "-I$1" % csrcDir.}
{.compile: csrcDir / "parser.c".}
proc treeSitterC*(): ptr TSLanguage {.importc: "tree_sitter_c".}
const cppsrcDir = cacheDir / "treesitter_cpp" / "src"
{.compile: cppsrcDir / "parser_cpp.c".}
{.compile: cppsrcDir / "scanner.cc".}
proc treeSitterCpp*(): ptr TSLanguage {.importc: "tree_sitter_cpp".}
proc getLineCol*(code: var string, node: TSNode): tuple[line, col: int] =
# Get line number and column info for node
let
point = node.tsNodeStartPoint()
result.line = point.row.int + 1
result.col = point.column.int + 1
proc getName*(node: TSNode): string {.inline.} =
if not node.isNil:
return $node.tsNodeType()
proc getNodeVal*(code: var string, node: TSNode): string =
if not node.isNil:
return code[node.tsNodeStartByte() .. node.tsNodeEndByte()-1]
proc getNodeVal*(gState: State, node: TSNode): string =
gState.code.getNodeVal(node)
proc getAtom*(node: TSNode): TSNode =
if not node.isNil:
# Get child node which is topmost atom
if node.getName() in gAtoms:
return node
elif node.len != 0:
if node[0].getName() in ["type_qualifier", "comment"]:
# Skip const, volatile
if node.len > 1:
return node[1].getAtom()
else:
return
else:
return node[0].getAtom()
proc getStartAtom*(node: TSNode): int =
if not node.isNil:
# Skip const, volatile and other type qualifiers
for i in 0 .. node.len - 1:
if node[i].getAtom().getName() notin gAtoms:
result += 1
else:
break
proc getConstQualifier*(gState: State, node: TSNode): bool =
# Check if node siblings have type_qualifier = `const`
var
curr = node.tsNodePrevNamedSibling()
while not curr.isNil:
# Check previous siblings
if curr.getName() == "type_qualifier" and
gState.getNodeVal(curr) == "const":
return true
curr = curr.tsNodePrevNamedSibling()
# Check immediate next sibling
curr = node.tsNodePrevNamedSibling()
if curr.getName() == "type_qualifier" and
gState.getNodeVal(curr) == "const":
return true
proc getXCount*(node: TSNode, ntype: string, reverse = false): int =
if not node.isNil:
# Get number of ntype nodes nested in tree
var
cnode = node
while ntype in cnode.getName():
result += 1
if reverse:
cnode = cnode.tsNodeParent()
else:
if cnode.len != 0:
if cnode[0].getName() == "type_qualifier":
# Skip const, volatile
if cnode.len > 1:
cnode = cnode[1]
else:
break
else:
cnode = cnode[0]
else:
break
proc getPtrCount*(node: TSNode, reverse = false): int =
node.getXCount("pointer_declarator", reverse)
proc getArrayCount*(node: TSNode, reverse = false): int =
node.getXCount("array_declarator")
proc getDeclarator*(node: TSNode): TSNode =
if not node.isNil:
# Return if child is a function or array declarator
if node.getName() in ["function_declarator", "array_declarator"]:
return node
elif node.len != 0:
return node[0].getDeclarator()
proc getVarargs*(node: TSNode): bool =
# Detect ... and add {.varargs.}
#
# `node` is the param list
#
# ... is an unnamed node, second last node and ) is last node
let
nlen = node.tsNodeChildCount()
if nlen > 1.uint32:
let
nval = node.tsNodeChild(nlen - 2.uint32).getName()
if nval == "...":
result = true
proc printLisp*(code: var string, root: TSNode): string =
var
node = root
nextnode: TSNode
depth = 0
while true:
if not node.tsNodeIsNull() and depth > -1:
result &= spaces(depth)
let
(line, col) = code.getLineCol(node)
result &= &"({$node.tsNodeType()} {line} {col} {node.tsNodeEndByte() - node.tsNodeStartByte()}"
let
val = code.getNodeVal(node)
if "\n" notin val and " " notin val:
result &= &" \"{val}\""
else:
break
if node.len() != 0:
result &= "\n"
nextnode = node[0]
depth += 1
else:
result &= ")\n"
nextnode = node.tsNodeNextNamedSibling()
if nextnode.tsNodeIsNull:
while true:
node = node.tsNodeParent()
depth -= 1
if depth == -1:
break
result &= spaces(depth) & ")\n"
if node == root:
break
if not node.tsNodeNextNamedSibling().tsNodeIsNull():
node = node.tsNodeNextNamedSibling()
break