ring-lang-rs 0.1.13

Rust bindings for the Ring programming language
Documentation
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
# gendoc.ring - Generate Markdown API documentation from .rf files
#
# Usage: ring gendoc.ring input.rf [output.md]
#
# Parses .rf configuration files and generates Markdown documentation.
# Supports: <meta>, <code>, <functions>, <struct>, <impl>, <constants>, <register>
#
# Author: Youssef Saeed (ysdragon)

load "stdlibcore.ring"

# Load parser library from the same directory as this script
cScriptPath = sysargv[2]
cScriptDir = ""
nLastSlash = 0
for i = 1 to len(cScriptPath)
    c = cScriptPath[i]
    if c = "/" or c = char(92)
        nLastSlash = i
    ok
next
if nLastSlash > 0
    cScriptDir = left(cScriptPath, nLastSlash)
ok
eval('load "' + cScriptDir + 'parsec_lib.ring"')

if len(sysargv) < 3
    ? "Usage: ring gendoc.ring input.rf [output.md]"
    ? ""
    ? "Generates Markdown API documentation from .rf configuration files."
    return
ok

cInputFile = sysargv[3]
cOutputFile = ""
if len(sysargv) >= 4
    cOutputFile = sysargv[4]
ok

if not fexists(cInputFile)
    ? "Error: File not found: " + cInputFile
    return
ok

cFileContent = read(cInputFile)
aLines = str2list(cFileContent)

$cLibPrefix = ""
$cCrateName = ""
$aFunctions = []
$aStructs = []
$aImpls = []
$aConstants = []

ParseFile(aLines)
cMarkdown = GenerateMarkdown()

if cOutputFile != ""
    write(cOutputFile, cMarkdown)
    ? "Generated: " + cOutputFile
else
    ? cMarkdown
ok

func ParseFile aLines
    nFlag = 0
    C_NONE = 0
    C_META = 1
    C_CODE = 2
    C_FUNCTIONS = 3
    C_STRUCT = 4
    C_IMPL = 5
    C_CONSTANTS = 6
    C_COMMENT = 7
    C_REGISTER = 8
    
    cStructData = ""
    cImplData = ""
    cLastComment = ""
    cLastSigComment = ""   # For "// name(params) - description" style comments
    aRegisteredFuncs = []  # Store ring_func! functions found in code
    
    for cLine in aLines
        cTrimmed = trim(cLine)
        
        if cTrimmed = "" and nFlag != C_CODE
            loop
        ok
        
        if left(cTrimmed, 1) = "#" and nFlag != C_CODE
            if left(cTrimmed, 2) = "# "
                cLastComment = substr(cTrimmed, 3)
            ok
            loop
        ok
        
        switch lower(cTrimmed)
        on "<meta>"
            nFlag = C_META
            loop
        on "</meta>"
            nFlag = C_NONE
            loop
        on "<code>"
            nFlag = C_CODE
            loop
        on "</code>"
            nFlag = C_NONE
            loop
        on "<functions>"
            nFlag = C_FUNCTIONS
            loop
        on "</functions>"
            nFlag = C_NONE
            loop
        on "<struct>"
            nFlag = C_STRUCT
            cStructData = ""
            loop
        on "</struct>"
            if cStructData != ""
                $aStructs + [cStructData, cLastComment]
                cLastComment = ""
            ok
            nFlag = C_NONE
            loop
        on "<impl>"
            nFlag = C_IMPL
            cImplData = ""
            loop
        on "</impl>"
            if cImplData != ""
                $aImpls + [cImplData, cLastComment]
                cLastComment = ""
            ok
            nFlag = C_NONE
            loop
        on "<constants>"
            nFlag = C_CONSTANTS
            loop
        on "</constants>"
            nFlag = C_NONE
            loop
        on "<comment>"
            nFlag = C_COMMENT
            loop
        on "</comment>"
            nFlag = C_NONE
            loop
        on "<register>"
            nFlag = C_REGISTER
            loop
        on "</register>"
            nFlag = C_NONE
            loop
        off
        
        switch nFlag
        on C_META
            ParseMeta(cTrimmed)
        on C_CODE
            # Detect pub fn for auto-wrapping
            if left(cTrimmed, 7) = "pub fn "
                lIsMethod = substr(cTrimmed, "&self") > 0 or 
                            substr(cTrimmed, "&mut self") > 0 or
                            substr(cTrimmed, "-> Self") > 0
                if not lIsMethod
                    nBracePos = substr(cTrimmed, "{")
                    if nBracePos > 0
                        cSig = trim(left(cTrimmed, nBracePos - 1))
                        cSig = trim(substr(cSig, 5))
                        $aFunctions + [cSig, cLastComment]
                        cLastComment = ""
                    ok
                ok
            ok
            # Detect ring_func! for manual functions
            if substr(cTrimmed, "ring_func!") > 0
                # Extract function name from ring_func!(ring_PREFIX_NAME, ...)
                nStart = substr(cTrimmed, "ring_func!(")
                if nStart > 0
                    cRest = substr(cTrimmed, nStart + 11)
                    nComma = substr(cRest, ",")
                    if nComma > 0
                        cRustFuncName = trim(left(cRest, nComma - 1))
                        # Store with the preceding signature comment
                        aRegisteredFuncs + [cRustFuncName, cLastSigComment, cLastComment]
                        cLastSigComment = ""
                        cLastComment = ""
                    ok
                ok
            ok
            # Capture /// doc comments
            if left(cTrimmed, 4) = "/// "
                cLastComment = substr(cTrimmed, 5)
            ok
            # Capture // signature comments like "// encode(list [, pretty]) - description"
            # Skip separator lines like "// ========"
            if left(cTrimmed, 3) = "// "
                cCommentContent = substr(cTrimmed, 4)
                # Check if it's a separator line - don't reset sig comment for these
                lIsSeparator = (substr(cCommentContent, "===") > 0 or 
                               substr(cCommentContent, "---") > 0 or
                               substr(cCommentContent, "***") > 0)
                if not lIsSeparator
                    # Check if it looks like a signature (has parens)
                    if substr(cCommentContent, "(") > 0 and substr(cCommentContent, ")") > 0
                        cLastSigComment = cCommentContent
                    ok
                ok
            ok
        on C_FUNCTIONS
            if left(lower(cTrimmed), 3) = "fn "
                $aFunctions + [cTrimmed, cLastComment]
                cLastComment = ""
            ok
        on C_STRUCT
            cStructData += cTrimmed + nl
        on C_IMPL
            cImplData += cTrimmed + nl
        on C_CONSTANTS
            if cTrimmed != ""
                $aConstants + [cTrimmed, cLastComment]
                cLastComment = ""
            ok
        on C_REGISTER
            if cTrimmed != "" and left(cTrimmed, 1) != "#"
                # Parse register line: "name" or "ring_name => rust_name"
                nArrow = substr(cTrimmed, "=>")
                if nArrow > 0
                    cRingName = trim(left(cTrimmed, nArrow - 1))
                    cRustName = trim(substr(cTrimmed, nArrow + 2))
                else
                    cRingName = cTrimmed
                    # Default rust name: ring_PREFIX_name
                    cRustName = "ring_" + $cLibPrefix + "_" + cRingName
                ok
                # Look up in aRegisteredFuncs for signature/description
                cSig = "fn " + cRingName + "(...)"
                cDesc = "Registered function"
                nFuncCount = len(aRegisteredFuncs)
                for i = 1 to nFuncCount
                    aFunc = aRegisteredFuncs[i]
                    if aFunc[1] = cRustName
                        # Found it! Parse the signature comment
                        if aFunc[2] != ""
                            # Parse "name(params) - description" or "name(params)"
                            cSigComment = aFunc[2]
                            nDash = substr(cSigComment, " - ")
                            if nDash > 0
                                cSigPart = trim(left(cSigComment, nDash - 1))
                                cDesc = trim(substr(cSigComment, nDash + 3))
                            else
                                cSigPart = cSigComment
                            ok
                            cSig = "fn " + cSigPart
                        ok
                        # Use doc comment if no sig comment
                        if aFunc[3] != "" and cDesc = "Registered function"
                            cDesc = aFunc[3]
                        ok
                        exit
                    ok
                next
                $aFunctions + [cSig, cDesc]
            ok
        off
    next

func ParseMeta cLine
    # Use parser
    ParseMetaLine(cLine)

func GenerateMarkdown
    cMD = ""
    cPrefix = $cLibPrefix
    if cPrefix != ""
        cPrefix += "_"
    ok
    
    cMD += "# " + upper(left($cLibPrefix, 1)) + substr($cLibPrefix, 2) + " API Reference" + nl
    cMD += nl
    
    if $cCrateName != ""
        cMD += "> Wraps: `" + $cCrateName + "`" + nl
        cMD += nl
    ok
    
    cMD += "**Function prefix:** `" + cPrefix + "`" + nl
    cMD += nl
    
    if len($aFunctions) > 0
        cMD += "## Functions" + nl
        cMD += nl
        cMD += "| Function | Description |" + nl
        cMD += "|----------|-------------|" + nl
        
        # Deduplicate functions by name
        aSeenFuncs = []
        for aFunc in $aFunctions
            cSig = aFunc[1]
            cDesc = aFunc[2]
            aInfo = ParseFunctionSig(cSig)
            cName = aInfo[1]
            cParams = aInfo[2]
            cReturn = aInfo[3]
            
            # Skip if already seen
            lSeen = false
            for cSeenName in aSeenFuncs
                if cSeenName = cName
                    lSeen = true
                    exit
                ok
            next
            if lSeen loop ok
            aSeenFuncs + cName
            
            cRingName = "`" + cPrefix + cName + "(" + FormatParams(cParams) + ")`"
            if cReturn != "" and cReturn != "()"
                cRingName += " → " + FormatType(cReturn)
            ok
            
            cMD += "| " + cRingName + " | " + cDesc + " |" + nl
        next
        cMD += nl
    ok
    
    if len($aStructs) > 0
        cQ = char(34)  # Double quote character
        cMD += "## Classes" + nl
        cMD += nl
        cMD += "Classes are generated Ring wrappers around the underlying structs." + nl
        cMD += nl
        cMD += "```ring" + nl
        cMD += "load " + cQ + $cLibPrefix + "_classes.ring" + cQ + nl
        cMD += "```" + nl
        cMD += nl
        
        for aStruct in $aStructs
            cData = aStruct[1]
            cDesc = aStruct[2]
            aInfo = ParseStruct(cData)
            cStructName = aInfo[1]
            aFields = aInfo[2]
            
            cMD += "### " + cStructName + " Class" + nl
            cMD += nl
            if cDesc != ""
                # Replace "struct" with "class" in description for documentation
                cDesc = substr(cDesc, " structs ", " classes ")
                cDesc = substr(cDesc, " struct ", " class ")
                cDesc = substr(cDesc, "Struct ", "Class ")
                cMD += cDesc + nl
                cMD += nl
            ok
            
            cLower = lower(cStructName)
            
            aImplMethods = GetImplMethods(cStructName)
            lHasCustomNew = false
            aNewParams = []
            cNewDesc = ""
            for aMethod in aImplMethods
                if aMethod[1] = "new"
                    lHasCustomNew = true
                    aNewParams = aMethod[2]
                    cNewDesc = aMethod[4]
                ok
            next
            
            # Constructor
            cMD += "**Constructor**" + nl
            cMD += nl
            cMD += "```ring" + nl
            if lHasCustomNew
                cMD += "obj = new " + cStructName + "(" + FormatParams(aNewParams) + ")" + nl
            else
                cMD += "obj = new " + cStructName + "()" + nl
            ok
            cMD += "```" + nl
            cMD += nl
            
            # Properties (from fields)
            if len(aFields) > 0
                cMD += "**Properties**" + nl
                cMD += nl
                cMD += "| Property | Type | Getter | Setter |" + nl
                cMD += "|----------|------|--------|--------|" + nl
                
                for aField in aFields
                    cFieldName = aField[1]
                    cFieldType = aField[2]
                    # Ring class style: obj.fieldName() and obj.setFieldName(value)
                    cGetter = "`obj." + cFieldName + "()`"
                    cSetterName = "set" + upper(left(cFieldName, 1)) + substr(cFieldName, 2)
                    cSetter = "`obj." + cSetterName + "(value)`"
                    cMD += "| " + cFieldName + " | " + FormatType(cFieldType) + " | " + cGetter + " | " + cSetter + " |" + nl
                next
                cMD += nl
            ok
            
            # Methods (excluding new, getters/setters already shown in properties)
            # Separate instance methods from static methods
            aMethods = []
            aStaticMethods = []
            for aMethod in aImplMethods
                cMethodName = aMethod[1]
                lIsStatic = aMethod[5]  # 5th element is static flag
                # Skip constructor
                if cMethodName = "new" loop ok
                # Skip getters that match field names (already in properties)
                lIsFieldGetter = false
                lIsFieldSetter = false
                for aField in aFields
                    if cMethodName = "get_" + aField[1]
                        lIsFieldGetter = true
                        exit
                    ok
                    if cMethodName = "set_" + aField[1]
                        lIsFieldSetter = true
                        exit
                    ok
                next
                if not lIsFieldGetter and not lIsFieldSetter
                    if lIsStatic
                        aStaticMethods + aMethod
                    else
                        aMethods + aMethod
                    ok
                ok
            next
            
            if len(aMethods) > 0
                cMD += "**Methods**" + nl
                cMD += nl
                cMD += "| Method | Description |" + nl
                cMD += "|--------|-------------|" + nl
                
                for aMethod in aMethods
                    cMethodName = aMethod[1]
                    aParams = aMethod[2]
                    cReturn = aMethod[3]
                    cMethodDesc = aMethod[4]
                    
                    # Ring class style: obj.methodName(params)
                    cCall = "`obj." + cMethodName + "("
                    if len(aParams) > 0
                        cCall += FormatParams(aParams)
                    ok
                    cCall += ")`"
                    if cReturn != "" and cReturn != "()" and cReturn != "Self"
                        cCall += " → " + FormatType(cReturn)
                    ok
                    
                    cMD += "| " + cCall + " | " + cMethodDesc + " |" + nl
                next
                cMD += nl
            ok
            
            if len(aStaticMethods) > 0
                cMD += "**Static Methods**" + nl
                cMD += nl
                cMD += "| Method | Description |" + nl
                cMD += "|--------|-------------|" + nl
                
                for aMethod in aStaticMethods
                    cMethodName = aMethod[1]
                    aParams = aMethod[2]
                    cReturn = aMethod[3]
                    cMethodDesc = aMethod[4]
                    
                    # Static style: prefix_structname_method(params)
                    cCall = "`" + cPrefix + cLower + "_" + cMethodName + "("
                    if len(aParams) > 0
                        cCall += FormatParams(aParams)
                    ok
                    cCall += ")`"
                    if cReturn != "" and cReturn != "()" and cReturn != "Self"
                        cCall += " → " + FormatType(cReturn)
                    ok
                    
                    cMD += "| " + cCall + " | " + cMethodDesc + " |" + nl
                next
                cMD += nl
            ok
            
            # Destructor
            cMD += "**Destructor**" + nl
            cMD += nl
            cMD += "```ring" + nl
            cMD += "obj.delete()" + nl
            cMD += "```" + nl
            cMD += nl
            
            # Example usage
            cMD += "**Example**" + nl
            cMD += nl
            cMD += "```ring" + nl
            cMD += "load " + cQ + $cLibPrefix + "_classes.ring" + cQ + nl
            cMD += nl
            if lHasCustomNew and len(aNewParams) > 0
                cExampleArgs = ""
                for i = 1 to len(aNewParams)
                    aParam = aNewParams[i]
                    cParamType = aParam[2]
                    if i > 1 cExampleArgs += ", " ok
                    if cParamType = "String" or cParamType = "&str"
                        cExampleArgs += cQ + "example" + cQ
                    elseif substr(cParamType, "i") > 0 or substr(cParamType, "u") > 0
                        cExampleArgs += "0"
                    elseif substr(cParamType, "f") > 0
                        cExampleArgs += "0.0"
                    elseif cParamType = "bool"
                        cExampleArgs += "true"
                    else
                        cExampleArgs += "..."
                    ok
                next
                cMD += "obj = new " + cStructName + "(" + cExampleArgs + ")" + nl
            else
                cMD += "obj = new " + cStructName + "()" + nl
            ok
            if len(aFields) > 0
                cField = aFields[1]
                cMD += "? obj." + cField[1] + "()    # Get " + cField[1] + nl
            ok
            if len(aMethods) > 0
                aMethod = aMethods[1]
                cMethodName = aMethod[1]
                aParams = aMethod[2]
                cReturn = aMethod[3]
                cCallExample = "obj." + cMethodName + "("
                for i = 1 to len(aParams)
                    aParam = aParams[i]
                    cParamType = aParam[2]
                    if i > 1 cCallExample += ", " ok
                    if cParamType = "String" or cParamType = "&str"
                        cCallExample += cQ + "test" + cQ
                    elseif substr(cParamType, "i") > 0 or substr(cParamType, "u") > 0
                        cCallExample += "0"
                    elseif substr(cParamType, "f") > 0
                        cCallExample += "0.0"
                    elseif cParamType = "bool"
                        cCallExample += "true"
                    else
                        cCallExample += "..."
                    ok
                next
                cCallExample += ")"
                if cReturn != "" and cReturn != "()" and cReturn != "Self"
                    cMD += "? " + cCallExample + nl
                else
                    cMD += cCallExample + nl
                ok
            ok
            cMD += "obj.delete()" + nl
            cMD += "```" + nl
            cMD += nl
        next
    ok
    
    if len($aConstants) > 0
        cMD += "## Constants" + nl
        cMD += nl
        cMD += "| Constant | Type | Getter |" + nl
        cMD += "|----------|------|--------|" + nl
        
        for aConst in $aConstants
            cConstLine = aConst[1]
            nColonPos = substr(cConstLine, ":")
            if nColonPos > 0
                cConstName = trim(left(cConstLine, nColonPos - 1))
                cConstType = trim(substr(cConstLine, nColonPos + 1))
                cGetter = "`" + cPrefix + "get_" + lower(cConstName) + "()`"
                cMD += "| " + cConstName + " | " + FormatType(cConstType) + " | " + cGetter + " |" + nl
            ok
        next
        cMD += nl
    ok
    
    cMD += "---" + nl
    cMD += "*Generated by gendoc.ring*" + nl
    
    return cMD

func ParseFunctionSig cSig
    # Use parser
    return ParseFuncSignature(cSig)

func ParseParams cParamsStr
    # Use parser
    return ParseFuncParams(cParamsStr)

func ParseStruct cData
    # Use parser
    return ParseStructDef(cData)

func GetImplMethods cStructName
    aMethods = []
    
    for aImpl in $aImpls
        cData = aImpl[1]
        aBasic = ParseImplDef(cData)
        cImplName = aBasic[1]
        
        if cImplName != cStructName loop ok
        
        # Parse impl data line by line to capture /// comments
        aLines = str2list(cData)
        cLastDocComment = ""
        
        for cLine in aLines
            cLine = trim(cLine)
            
            # Capture /// doc comments
            if left(cLine, 4) = "/// "
                cLastDocComment = substr(cLine, 5)
                loop
            ok
            
            # Match method line with parsed methods
            if left(cLine, 7) = "pub fn "
                cSig = trim(substr(cLine, 5))
                aInfo = ParseFuncSignature(cSig)
                cMethodName = aInfo[1]
                aParams = aInfo[2]
                cReturn = aInfo[3]
                
                # Check if static (no &self or &mut self)
                lIsStatic = true
                aFilteredParams = []
                for aParam in aParams
                    cParamName = aParam[1]
                    if cParamName = "&self" or cParamName = "&mut self" or cParamName = "self"
                        lIsStatic = false
                    else
                        aFilteredParams + aParam
                    ok
                next
                
                aMethods + [cMethodName, aFilteredParams, cReturn, cLastDocComment, lIsStatic]
                cLastDocComment = ""
            ok
        next
    next
    
    return aMethods

func FormatParams aParams
    # Use formatter
    return FormatParamList(aParams)

func FormatType cType
    # Use formatter
    return FormatRustType(cType)