EricFromCanada.bitbucket.org / lasso / sphinxifier.lasso

  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
#!/usr/bin/lasso9
/*[*/
/*
  Given a set of Lasso files or element names, outputs reference docs for each
  element for the Lasso domain for Sphinx. Since Lasso defaults to not retaining
  docComments, prefix with `env LASSO9_RETAIN_COMMENTS=1` when running from the
  command line. Generates reST markup like so (with extra line breaks):

  method:: signature
    docstring                     <-- docComment content before @-prefixed attribute lines
    :param name: description          <-- from "@param name description" in docComment
    :param type name: description     <-- type inserted if not ::any
    :param ...: description        <-- if signature has ... & docComment has @param rest or @param ...
    :param restname: description    <-- if signature has ...restname & docComment has @param restname
    :return: description          <-- from "@return description" in docComment
    :rtype: return type           <-- rtype inserted if return type not ::any
    :see: resource            <-- from "@see resource" in docComment
  trait:: name
    docstring
    :see: resource
    :import: traitname
    require:: signature
      ...                 <-- same as method::
    provide:: signature
      ...                 <-- same as method::
  type:: name
    docstring
    :see: resource
    :parent: typename
    member:: signature
      ...                 <-- same as method::
    :import: traitname
    provide:: signature
      ...                 <-- same as method::
  thread:: name
    ...               <-- same as type::

  Known limitations:
  - won't generate :import: lines for traits added later with ->addTrait
  - Lasso applies a docComment to only the first require statement in a group
  - Lasso has no way to show which member methods are public/protected/private (#7494)
  - a trait will see its requires disappear as imports are added (#7581)
  - data elements and therefore automatic getters/setters can't have docstrings
  - little error handling, e.g. when finding @param lines with no matching parameter
  - uses auto-collect; might be faster with output to variable instead
*/

not sys_getenv('LASSO9_RETAIN_COMMENTS') ?
  fail('This program requires the LASSO9_RETAIN_COMMENTS environment variable be set to 1')
$argc == 1 ?
  fail('Specify --find=<type or trait regex> and/or one or more Lasso files to read as arguments')

/**!
  Type containing description and attributes of a given tag's doc comment.
  @author   Eric Knibbe
  @see      http://lassoguide.com/syntax/literals.html
  @see      http://sphinx-doc.org/domains.html#info-field-lists
*/
define docObject => type {
  data indent             // string to use as indent
  data description = ''   // first block of text before @attribute lines
  data paramsorder = array()  // each param's name, since params is an unordered container
  data paramskeyed = array()  // whether each param is a keyword parameter
  data params = map()     // each is ('pname' = pair(::ptype, 'pdesc')) from #element & @param lines
  data others = array()   // each is (pair('attribute', 'value')) from other @attribute lines
  data result = pair()    // (::rtype, 'rdesc') from #element & @return

  // #element can be a tag or signature, or anything supporting ->docComment
  public onCreate(element, indent::string='   ') => {
    .'indent' = #indent

    // fill params with names & types from ->paramDescs, ->restName, ->returnType
    // only applies to signatures, as types & traits won't have params or return
    if (#element->type == ::signature) => {
      with parray in #element->paramDescs
      do {
        .'paramsorder'->insert(#parray->first)
        .'paramskeyed'->insert(#parray->get(3)->bitTest(2))
        .'params'->insert(pair(#parray->first, pair(#parray->second, null)))
      }
      if (#element->restName != void) => {
        .'paramsorder'->insert(#element->restName == ::rest ? '...' | #element->restName->asString)
        .'params'->insert(pair(
            (#element->restName == ::rest ? '...' | #element->restName->asString),
            pair(::rest, null)
        ))
      }
      #element->returnType != ::any ? .'result'->first = #element->returnType
    }

    // fill params & others with data parsed from @attribute lines
    local(      // remove delimiters, trim & collapse whitespace but preserve paragraph breaks
      components = #element->docComment->replace(regexp('^/\\*\\*!'),' ')
                                        &removeTrailing('*/')
                                        &removeTrailing('*')
                                        &replace(regexp('[ \\t]*(\\n|\\r\\n?)[ \\t]*'),'\n')
                                        &replace(regexp('[ \\t]+|(?<!\\n)\\n(?!\\n)'),' ')
                                        &replace(regexp('\\n+@'),' @') // first @ always needs leading space
                                        &split(' @')
    )   // array('description','param name Words about parameter','return Words about result')
    #components->first != null ? .'description' = #components->first->trim&;
    // loop through #components, splitting each on space and checking the first result
    with item in #components
    skip 1
    let words = #item->trim&split(' ')  // array('param','name','Words','about','parameter')
    let word1 = string_removetrailing(#words->first, ':')   // attribute type
    let word2 = string_removetrailing(#words->second, ':')  // name if attribute is param, otherwise description
    do {
      local(ptype = ::any)
      if (#word1 == 'param') => {
        protect => {        // in case name following @param is misspelled
          // first check if the given @param name matches the signature's ->restName
          if (#element->restName != void &&
              #word2 == #element->restName ||
              #word2 == '...') => {
            .'params'->insert(pair(
                (#word2 == 'rest' ? '...' | #word2),
                pair(#ptype, #words->remove(1,2)&join(' '))
            ))
          else
            #ptype = .'params'->get(#word2)->first
            .'params'->insert(pair(#word2, pair(#ptype, #words->remove(1,2)&join(' '))))
          }
        }
      else (#word1 == 'return')
        .'result'->second = #words->remove(1)&join(' ')
      else
        .'others'->insert(pair(#word1, #words->remove(1)&join(' ')))
      }
    }
  }

  /**! @return description block, wrapped and indented */
  public description()::string => {
    return (.'description'->size != 0 ? string_wrap(.'description', .'indent') + '\n' | '')
  }

  /**! @return parameter descriptions as roles */
  public params()::string => {
    local(attributes = '')
    protect => {
      pairup(.'paramsorder', .'paramskeyed')->forEach => {
        local(name = #1->first)
        #attributes->append(.'indent' + ':param')
        if (.'params'->get(#name)->first != ::any &&
            .'params'->get(#name)->first != ::rest) => {
          #attributes->append(' ' + .'params'->get(#name)->first->asString)
        }
        #attributes->append(' ' + (#1->second ? '-') + #name->asString + ':')
        if (.'params'->get(#name)->second != null) => {
          #attributes->append(' ' + .'params'->get(#name)->second->asString + '\n')
        else
          #attributes->append('\n')
        }
      }
    }
    return #attributes
  }

  /**! @return other doc comment attributes as roles */
  public others()::string => {
    local(attributes = '')
    if (.'result'->second != null) => {
      #attributes->append(.'indent' + ':return: ' + .'result'->second->asString + '\n')
    }
    if (.'result'->first != null) => {
      #attributes->append(.'indent' + ':rtype: `' + .'result'->first->asString + '`\n')
    }
    with attr in .'others'
    do {
      #attributes->append(.'indent' + ':' + #attr->first + ': ' + #attr->second + '\n')
    }
    return #attributes
  }
}

/**!
  Wraps the given string to the specified length.
  Modified to accept a string for indenting each line.
  (indent was originally a keyword parameter, but issue #7408 prevented that)
  @see http://www.lassosoft.com/tagswap/detail/string_wrap
*/
define string_wrap(
    text,
    indent::string='',
    length::integer=80,
    linebreak::string='\n',
    trim::boolean=true,
    -priority='replace'
    ) => {
  local(
    'in' = string(#text)->trim&,
    'out' = string
  )
  #length -= #indent->size + 1
  #in->replace('\r\n','\n')&replace('\r','\n')
  iterate(#in->split('\n'), local('i')) => {
    local('line' = #i)
    #trim ? #line->trim
    if(#line->size < #length) => {
      #out += #indent + #line + #linebreak
    else
      local(
        'lineIn' = #line,
        'lineOut' = string
      )
      while(#lineIn->size > #length) => {
        local('offset' = #length)
        while(#lineIn->size > #offset &&
              #offset > 0 &&
              !#lineIn->isspace(#offset)) => {
          #offset -= 1
        }
        #offset == 0 ? #offset = #length
        local('chunk') = #lineIn->substring(1, #offset)
        #lineOut += #indent + #chunk + #linebreak
        #lineIn->removeleading(#chunk)
      }
      #lineIn->size ? #lineOut += #indent + #lineIn + #linebreak
      #out += #lineOut
    }
  }
  return(#out)
}

/**!
  A reST-friendly output method for the signature type.
  @return   signature as reST string
  @author   Eric Knibbe
*/
define signature->asReString => {
  local(
    output = .methodName->asString + '(',
    num = 0,
    opt = false,
    size = .paramDescs->size
  )
  with param in .paramDescs
  let name = #param->get(1)
  let type = #param->get(2)
  let flags = #param->get(3)  // 1st bit: optional param, 2nd bit: keyword param
  do {
    if (!#opt && #flags->bitTest(1)) => {
      #opt = true
      #output->append('[')
    }
    if (#opt && !#flags->bitTest(1)) => {
      #opt = false
      #output->append(']')
    }
    #num != 0 ?
      #output->append(', ')
    #num++
    #flags->bitTest(2) ?
      #output->append('-')
    #output->append(#name->asString)
    #type != ::any ?
      #output->append('::' + #type->asString)
    #opt && #num==#size && !.restName ?
      #output->append(']')
    !#opt && #num==#size && .restName ?
      #output->append('[')
  }
  if (.restName) => {
    #size == 0 ?
      #output->append('[') | #output->append(', ')
    #output->append('...')
    .restName != ::rest ?
      #output->append(.restName->asString)
    #output->append(']')
  }
  #output->append(')')
  .returnType != ::any ?
    #output->append('::' + .returnType)
  return #output
}

/**!
  Write docs for a given tag.
  @param element    the subject of the docs, can be ::tag or ::signature
  @param directive  what directive to use, e.g. type::
  @param nesting    what level to nest the output (0 = no nesting)
  @author   Eric Knibbe
*/
define writeDocs(element, directive::string, nesting::integer=0) => {^

  // check if #element is a thread or thread's type
  if (#element->type == ::tag) => {
    if (#element->asString->endsWith('_thread$')) => {
      #directive = 'thread'
    else (#element->gettype->parent->asString->endsWith('_thread$'))
      #directive = 'thread'
      #element = #element->gettype->parent  // given thread's type, switch to thread
    }
  }

  local(
    indent = '   ',
    docElement = docObject(#element, #indent*(#nesting+1))
  )

  // initial directive
  '\n' + #indent*#nesting + '.. ' + #directive + ':: '
  #element->type == ::signature ?
    #element->asReString + '\n\n' | #element->asString->removeTrailing('_thread$')& + '\n\n'

  // items from docComment
  #docElement->description
  #docElement->params
  #docElement->others

  if (#directive == 'trait') => {

    // roles for imported traits
    if (#element->getType->subTraits->size != 0) => {^
      local(importlist =
        with trait in #element->getType->subTraits
        order by #trait->asString
        select #trait
      )
      #indent*(#nesting+1) + ':import: `' + #importlist->join('`, `') + '`\n'
    ^}

    // require directive
    with require in #element->getType->requires
    where #require->typeName == #element
    do {^
      writeDocs(#require, 'require', #nesting+1)
    ^}

    // provide directive
    with provide in #element->getType->provides
    where #provide->typeName == #element
    do {^
      writeDocs(#provide, 'provide', #nesting+1)
    ^}

  else (#directive == 'type' || #directive == 'thread')

    // role for parent type
    if (#element->getType->parent != ::null) => {^
      #indent*(#nesting+1) + ':parent: `' + #element->getType->parent->asString + '`\n'
    ^}

    // switch back to thread's type
    #directive == 'thread' ? #element = tag(#element->asString->removeTrailing('_thread$')&)

    // trait block within type
    if (#element->getType->trait != void &&
        #element->getType->trait->asString != 'any' &&
        #element->getType->trait->asString !>> #element->getType->parent->asString
        ) => {
      local(imports = array())
      if (#element->getType->trait->subTraits->size != 0 &&
          #element->getType->trait->subTraits->first->asString != '_'
          ) => {
        if (#element->getType->trait->subTraits->first->asString->isAlpha(1)) => {
          local(traitlist = #element->getType->trait->subTraits)
        else    // addTrait has been used on this type
          local(traitlist = #element->getType->trait->subTraits->first->subtraits)
        }
        with each in #traitlist
        where #each != any
        do {
          if (#each->asString->isalpha(1)) => {
            #imports->insert(#each)
          else (#each->asString == ('_' || '$$trait.' + #element->asString))
            #imports->merge(#each->subTraits)
          }
        }
      }

      if (#imports->size) => {^
        local(importlist =
          with trait in #imports
          order by #trait->asString
          select #trait
        )
        #indent*(#nesting+1) + ':import: `' + #importlist->join('`, `') + '`\n'
      ^}

      // trait provide within type
      with provide in #element->getType->trait->provides
      where #provide->typeName->asString == '$$trait.' + #element->asString
      order by #provide->methodName     // since the ordering can be random
      do {^
        writeDocs(#provide, 'provide', #nesting+1)
      ^}
    }

    // member directive
    // refine to detect `data` candidates 
    with member in #element->getType->listMethods
    let m_name = #member->methodName->asString
    where #member->typeName == #element
    where not #m_name->beginsWith("'") // skip 'x'() but not _x() x=() x()
    order by #m_name->isAlpha(1), #m_name
    do {^
      writeDocs(#member, 'member', #nesting+1)
    ^}

  }

^}

/*
  This script is run with arguments specifying language elements and/or files to
  generate reST markup for. Each new type, trait, and method can be read off the
  end of the lists returned by the sys_list* methods.
*/
iterate($argv) => {
  loop_count == 1 ? loop_continue
  $argv->first->endsWith(loop_value) ? loop_continue  // prevent script from reading itself
  local(
    typecount_orig = sys_listTypes->size,     // staticarray of tags
    traitcount_orig = sys_listTraits->size,   // staticarray of tags
    methodcount_orig = sys_listUnboundMethods->size,  // staticarray of signatures
    currentfile = file(loop_value),
    typelist = array(),
    traitlist = array()
  )

  // if argument is --find=<type or trait regex>, include the results in the output
  if (loop_value->beginsWith('--find=')) => {
    local(findarg = '^(?![$])' + loop_value->sub(8)->replace('"','')&replace("'",'')&)
    sys_listTypes->forEach => {
      regexp(-find=#findarg, -input=#1->asString, -ignoreCase)->matches ?
        stdoutnl('\n' + '='*#1->asString->size +
                 '\n' + #1->asString +
                 '\n' + '='*#1->asString->size +
                 '\n' + writeDocs(#1, 'type'))
    }
    sys_listTraits->forEach => {
      regexp(-find=#findarg, -input=#1->asString, -ignoreCase)->matches ?
        stdoutnl('\n' + '='*#1->asString->size +
                 '\n' + #1->asString +
                 '\n' + '='*#1->asString->size +
                 '\n' + writeDocs(#1, 'trait'))
    }
    loop_continue
  }

  // read each specified file
  sourcefile(#currentfile, -autoCollect=false)->invoke
  stdoutnl(
    '\n' + '='*#currentfile->name->size +
    '\n' + #currentfile->name +
    '\n' + '='*#currentfile->name->size
  )    // print the filename as a heading

  // create lists of new types & traits
  with type in sys_listTypes
  skip #typecount_orig
  where not #type->asString->endsWith('$')  // skip thread objects
  do {
    #typelist->insert(#type)
  }
  with trait in sys_listTraits
  skip #traitcount_orig
  where not #trait->asString->beginsWith('$') // skip traits made by combining other traits
  do {
    #traitlist->insert(#trait)
  }

  // write markup for new unbound methods (must occur before writing types, or this file's methods are included)
  with method in sys_listUnboundMethods
  skip #methodcount_orig
  where #method->methodName->asString->isalpha(1)     // skip private methods
  where not #typelist->contains(#method->methodName)  // skip auto-generated type & trait methods
  where not #traitlist->contains(#method->methodName)
  do {
    stdoutnl(writeDocs(#method, 'method'))
  }

  // write markup for new types
  with type in #typelist
  do {
    stdoutnl(writeDocs(#type, 'type'))
  }

  // write markup for new traits
  with trait in #traitlist
  do {
    stdoutnl(writeDocs(#trait, 'trait'))
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.