Skip to main content
summaryrefslogtreecommitdiffstats
blob: ab947de2835b0da668da2f8482664af670c22883 (plain) (blame)
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
-------------------------------------------------------------------------------
-- Copyright (c) 2011-2013 Sierra Wireless and others.
-- All rights reserved. This program and the accompanying materials
-- are made available under the terms of the Eclipse Public License v1.0
-- which accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- Contributors:
--     Sierra Wireless - initial API and implementation
-------------------------------------------------------------------------------
local mlc = require ('metalua.compiler').new()
local gg = require 'metalua.grammar.generator'
local lexer = require 'metalua.grammar.lexer'
local mlp = mlc.parser

local M = {}            -- module
local lx                -- lexer used to parse tag
local registeredparsers -- table {tagname => {list de parsers}}

-- ----------------------------------------------------
-- copy key and value from one table to an other
-- ----------------------------------------------------
local function copykey(tablefrom, tableto)
  for key, value in pairs(tablefrom) do
    if key ~= "lineinfos" then
      tableto[key] = value
    end
  end
end

-- ----------------------------------------------------
-- Handle keyword and identifiers as word
-- ----------------------------------------------------
local function parseword(lx)
  local word = lx :peek()
  local tag = word.tag

  if tag=='Keyword' or tag=='Id' then
    lx:next()
    return  {tag='Word', lineinfo=word.lineinfo, word[1]}
  else
    return gg.parse_error(lx,'Id or Keyword expected')
  end
end

-- ----------------------------------------------------
-- parse an id
-- return a table {name, lineinfo)
-- ----------------------------------------------------
local idparser = gg.sequence({
  builder =  function (result)
    return { name = result[1][1] }
  end,
  parseword
})

-- ----------------------------------------------------
-- parse a modulename  (id.)?id
-- return a table {name, lineinfo)
-- ----------------------------------------------------
local modulenameparser = gg.list({
  builder =  function (result)
    local ids = {}
    for i, id in ipairs(result) do
      table.insert(ids,id.name)
    end
    return {name = table.concat(ids,".")}
  end,
  primary  = idparser,
  separators = '.'
})
-- ----------------------------------------------------
-- parse a typename  (id.)?id
-- return a table {name, lineinfo)
-- ----------------------------------------------------
local typenameparser = modulenameparser

-- ----------------------------------------------------
-- parse an internaltype ref
-- ----------------------------------------------------
local internaltyperefparser = gg.sequence({
  builder = function(result)
    return {tag = "typeref",type=result[1].name}
  end,
  "#", typenameparser
})

-- ----------------------------------------------------
-- parse an internal typeref, without the first #
-- ----------------------------------------------------
local sharplessinternaltyperefparser = gg.sequence({
  builder = function(result)
    return {tag = "typeref",type=result[1].name}
  end,
  typenameparser
})

-- ----------------------------------------------------
-- parse an external type ref
-- ----------------------------------------------------
local externaltyperefparser = gg.sequence({
  builder =  function(result)
    return {tag = "typeref",module=result[1].name,type=result[2].name}
  end,
  modulenameparser,"#", typenameparser
})

-- ----------------------------------------------------
-- enable recursive use of typeref parser
-- ----------------------------------------------------
local typerefparser,_typerefparser
typerefparser = function (...) return _typerefparser(...) end

-- ----------------------------------------------------
-- parse a structure type, without the first #
-- ----------------------------------------------------
local sharplesslisttyperefparser = gg.sequence({
  builder =  function(result)
    return {tag = "typeref", type="list", valuetype=result[1]}
  end,
  "list","<", typerefparser, ">"
})

-- ----------------------------------------------------
-- parse a map type, without the first #
-- ----------------------------------------------------
local sharplessmaptyperefparser = gg.sequence({
  builder =  function(result)
    return {tag = "typeref", type="map", keytype=result[1], valuetype=result[2]}
  end,
  "map","<", typerefparser, ",", typerefparser, ">"
})

-- ----------------------------------------------------
-- parse typeref stating with a #
-- The need to use the following parser is because the multisequence parser
-- works only if the given parsers doesn't start with the same keyword (here '#').
-- ----------------------------------------------------
local sharptyperefparser = gg.sequence({
  builder = function(result)
    return result[1]
  end,
  "#",
  gg.multisequence({
    sharplesslisttyperefparser,
    sharplessmaptyperefparser,
    sharplessinternaltyperefparser
  })
})

-- ----------------------------------------------------
-- parse a typeref
-- ----------------------------------------------------
_typerefparser =  gg.multisequence({
  sharptyperefparser,
  externaltyperefparser
})

-- ----------------------------------------------------
-- parse a list of typeref
-- return a list of table {name, lineinfo)
-- ----------------------------------------------------
local typereflistparser = gg.list({
  primary  = typerefparser,
  separators = ','
})

-- ----------------------------------------------------
-- TODO use a more generic way to parse (modifier if not always a typeref)
-- TODO support more than one modifier
-- ----------------------------------------------------
local modifiersparser = gg.sequence({
  builder =  function(result)
    return {[result[1].name]=result[2]}
  end,
  "[", idparser ,  "=" , internaltyperefparser , "]"
})

-- ----------------------------------------------------
-- parse a list tag
-- ----------------------------------------------------
local listparsers = {
  -- full parser
  gg.sequence({
    builder = function (result)
      return {type = result[1]}
    end,
    '@','list','<',typerefparser,'>'
  }),
}

-- ----------------------------------------------------
-- parse a map tag
-- ----------------------------------------------------
local mapparsers = {
  -- full parser
  gg.sequence({
    builder = function (result)
      return {keytype = result[1],valuetype = result[2]}
    end,
    '@','map','<',typerefparser,',',typerefparser,'>'
  }),
}

-- ----------------------------------------------------
-- parse a extends tag
-- ----------------------------------------------------
local extendsparsers = {
  -- full parser
  gg.sequence({
    builder = function (result)
      return {type = result[1]}
    end,
    '@','extends', typerefparser
  }),
}

-- ----------------------------------------------------
-- parse a callof tag
-- ----------------------------------------------------
local callofparsers = {
  -- full parser
  gg.sequence({
    builder = function (result)
      return {type = result[1]}
    end,
    '@','callof', internaltyperefparser
  }),
}

-- ----------------------------------------------------
-- parse a return tag
-- ----------------------------------------------------
local returnparsers = {
  -- full parser
  gg.sequence({
    builder =  function (result)
      return { types= result[1]}
    end,
    '@','return', typereflistparser
  }),
  -- parser without typerefs
  gg.sequence({
    builder =  function (result)
      return { types = {}}
    end,
    '@','return'
  })
}

-- ----------------------------------------------------
-- parse a param tag
-- ----------------------------------------------------
local paramparsers = {
  -- full parser
  gg.sequence({
    builder =  function (result)
      return { name = result[2].name, type = result[1]}
    end,
    '@','param', typerefparser, idparser
  }),

  -- reject the case were only a type without name
  gg.sequence({
    builder = function (result)
      return {tag="Error"}
    end,
    '@','param', '#'
  }),

  -- parser without type
  gg.sequence({
    builder = function (result)
      return { name = result[1].name}
    end,
    '@','param', idparser
  }),

  -- Parser for `Dots
  gg.sequence({
    builder = function (result)
      return { name = '...' }
    end,
    '@','param', '...'
  }),
}
-- ----------------------------------------------------
-- parse a field tag
-- ----------------------------------------------------
local fieldparsers = {
  -- full parser
  gg.sequence({
    builder =  function (result)
      local tag = {}
      copykey(result[1],tag)
      tag.type = result[2]
      tag.name = result[3].name
      return tag
    end,
    '@','field', modifiersparser, typerefparser, idparser
  }),

  -- reject the case where the type name is empty
  gg.sequence({
    builder = function (result)
      return {tag = "Error"}
    end,
    '@','field',modifiersparser, '#'
  }),

  -- parser without name
  gg.sequence({
    builder =   function (result)
      local tag = {}
      copykey(result[1],tag)
      tag.type = result[2]
      return tag
    end,
    '@','field', modifiersparser, typerefparser
  }),

  -- parser without type
  gg.sequence({
    builder =   function (result)
      local tag = {}
      copykey(result[1],tag)
      tag.name = result[2].name
      return tag
    end,
    '@','field', modifiersparser, idparser
  }),

  -- parser without type and name
  gg.sequence({
    builder =   function (result)
      local tag = {}
      copykey(result[1],tag)
      return tag
    end,
    '@','field', modifiersparser
  }),

  -- parser  without modifiers
  gg.sequence({
    builder =   function (result)
      return { name = result[2].name, type = result[1]}
    end,
    '@','field', typerefparser, idparser
  }),

  -- parser without modifiers and name
  gg.sequence({
    builder = function (result)
      return {type = result[1]}
    end,
    '@','field', typerefparser
  }),

  -- reject the case where the type name is empty
  gg.sequence({
    builder = function (result)
      return {tag = "Error"}
    end,
    '@','field', '#'
  }),

  -- parser without type and modifiers
  gg.sequence({
    builder = function (result)
      return { name = result[1].name}
    end,
    '@','field', idparser
  }),

  -- parser with nothing
  gg.sequence({
    builder = function (result)
      return {}
    end,
    '@','field'
  })
}

-- ----------------------------------------------------
-- parse a function tag
-- TODO use a more generic way to parse modifier !
-- ----------------------------------------------------
local functionparsers = {
  -- full parser
  gg.sequence({
    builder =   function (result)
      local tag = {}
      copykey(result[1],tag)
      tag.name = result[2].name
      return tag
    end,
    '@','function', modifiersparser, idparser
  }),

  -- parser without name
  gg.sequence({
    builder =   function (result)
      local tag = {}
      copykey(result[1],tag)
      return tag
    end,
    '@','function', modifiersparser
  }),

  -- parser without modifier
  gg.sequence({
    builder =   function (result)
      local tag = {}
      tag.name = result[1].name
      return tag
    end,
    '@','function', idparser
  }),

  -- empty parser
  gg.sequence({
    builder =   function (result)
      return {}
    end,
    '@','function'
  })
}

-- ----------------------------------------------------
-- parse a type tag
-- ----------------------------------------------------
local typeparsers = {
  -- full parser
  gg.sequence({
    builder =   function (result)
      return { name = result[1].name}
    end,
    '@','type',typenameparser
  }),
  -- parser without name
  gg.sequence({
    builder =   function (result)
      return {}
    end,
    '@','type'
  })
}

-- ----------------------------------------------------
-- parse a module tag
-- ----------------------------------------------------
local moduleparsers = {
  -- full parser
  gg.sequence({
    builder =   function (result)
      return { name = result[1].name }
    end,
    '@','module', modulenameparser
  }),
  -- parser without name
  gg.sequence({
    builder =   function (result)
      return {}
    end,
    '@','module'
  })
}

-- ----------------------------------------------------
-- parse a third tag
-- ----------------------------------------------------
local thirdtagsparser = gg.sequence({
  builder =   function (result)
    return { name = result[1][1] }
  end,
  '@', mlp.id
})
-- ----------------------------------------------------
-- init parser
-- ----------------------------------------------------
local function initparser()
  -- register parsers
  -- each tag name has several parsers
  registeredparsers  = {
    ["module"]   = moduleparsers,
    ["return"]   = returnparsers,
    ["type"]     = typeparsers,
    ["field"]    = fieldparsers,
    ["function"] = functionparsers,
    ["param"]    = paramparsers,
    ["extends"]  = extendsparsers,
    ["list"]     = listparsers,
    ["map"]      = mapparsers,
    ["callof"]   = callofparsers
  }

  -- create lexer used for parsing
  lx = lexer.lexer:clone()
  lx.extractors = {
    -- "extract_long_comment",
    -- "extract_short_comment",
    -- "extract_long_string",
    "extract_short_string",
    "extract_word",
    "extract_number",
    "extract_symbol"
  }

  -- Add dots as keyword
  local tagnames = { '...' }

  -- Add tag names as key word
  for tagname, _ in pairs(registeredparsers) do
    table.insert(tagnames,tagname)
  end
  lx:add(tagnames)

  return lx, parsers
end

initparser()

-- ----------------------------------------------------
-- get the string pattern to remove for each line of description
-- the goal is to fix the indentation problems
-- ----------------------------------------------------
local function getstringtoremove (stringcomment,commentstart)
  local _,_,capture = string.find(stringcomment,"\n?([ \t]*)@[^{]+",commentstart)
  if not capture then
    _,_,capture = string.find(stringcomment,"^([ \t]*)",commentstart)
  end
  capture = string.gsub(capture,"(.)","%1?")
  return capture
end

-- ----------------------------------------------------
-- parse comment tag partition and return table structure
-- ----------------------------------------------------
local function parsetag(part)
  if part.comment:find("^@") then
    -- check if the part start by a supported tag
    for tagname,parsers in pairs(registeredparsers) do
      if (part.comment:find("^@"..tagname)) then
        -- try the registered parsers for this tag
        local result
        for i, parser in ipairs(parsers) do
          local valid, tag = pcall(parser, lx:newstream(part.comment, tagname .. 'tag lexer'))
          if valid then
            -- add tagname
            tag.tagname = tagname

            -- add description
            local endoffset = tag.lineinfo.last.offset
            tag.description = part.comment:sub(endoffset+2,-1)
            return tag
          end
        end
      end
    end
  end
  return nil
end

-- ----------------------------------------------------
-- Parse third party tags.
--
-- Enable to parse a tag not defined in language.
-- So for, accepted format is: @sometagname adescription
-- ----------------------------------------------------
local function parsethirdtag( part )

  -- Check it there is someting to process
  if not part.comment:find("^@") then
    return nil, 'No tag to parse'
  end

  -- Apply parser
  local status, parsedtag = pcall(thirdtagsparser, lx:newstream(part.comment, 'Third party tag lexer'))
  if not status then
    return nil, "Unable to parse given string."
  end

  -- Retrieve description
  local endoffset = parsedtag.lineinfo.last.offset
  local tag = {
    description = part.comment:sub(endoffset+2,-1)
  }
  return parsedtag.name, tag
end

-- ---------------------------------------------------------
-- split string comment in several part
-- return list of {comment = string, offset = number}
-- the first part is the part before the first tag
-- the others are the part from a tag to the next one
-- ----------------------------------------------------
local function split(stringcomment,commentstart)
  local partstart = commentstart
  local result = {}

  -- manage case where the comment start by @
  -- (we must ignore the inline see tag @{..})
  local at_startoffset, at_endoffset = stringcomment:find("^[ \t]*@[^{]",partstart)
  if at_endoffset then
    partstart = at_endoffset-1 -- we start before the @ and the non '{' character
  end

  -- split comment
  -- (we must ignore the inline see tag @{..})
  repeat
    at_startoffset, at_endoffset = stringcomment:find("\n[ \t]*@[^{]",partstart)
    local partend
    if at_startoffset then
      partend= at_startoffset-1 -- the end is before the separator pattern (just before the \n)
    else
      partend = #stringcomment -- we don't find any pattern so the end is the end of the string
    end
    table.insert(result, { comment = stringcomment:sub (partstart,partend) ,
      offset = partstart})
    if at_endoffset then
      partstart = at_endoffset-1 -- the new start is befire the @ and the non { char
    end
  until not at_endoffset
  return result
end


-- ----------------------------------------------------
-- parse a comment block and return a table
-- ----------------------------------------------------
function M.parse(stringcomment)

  local _comment = {description="", shortdescription=""}

  -- clean windows carriage return
  stringcomment = string.gsub(stringcomment,"\r\n","\n")

  -- check if it's a ld comment
  -- get the begin of the comment
  -- ============================
  if not stringcomment:find("^-") then
    -- if this comment don't start by -, we will not handle it.
    return nil
  end

  -- retrieve the real start
  local commentstart = 2 --after the first hyphen
  -- if the first line is an empty comment line with at least 3 hyphens we ignore it
  local  _ , endoffset = stringcomment:find("^-+[ \t]*\n")
  if endoffset then
    commentstart = endoffset+1
  end

  -- clean comments
  -- ===================
  -- remove line of "-"
  stringcomment = string.sub(stringcomment,commentstart)
  -- clean indentation
  local pattern = getstringtoremove (stringcomment,1)
  stringcomment = string.gsub(stringcomment,"^"..pattern,"")
  stringcomment = string.gsub(stringcomment,"\n"..pattern,"\n")

  -- split comment part
  -- ====================
  local commentparts = split(stringcomment, 1)

  -- Extract descriptions
  -- ====================
  local firstpart = commentparts[1].comment
  if firstpart:find("^[^@]") or firstpart:find("^@{") then
    -- if the comment part don't start by @
    -- it's the part which contains descriptions
    -- (there are an exception for the in-line see tag @{..})
    local shortdescription, description = string.match(firstpart,'^(.-[.?])(%s.+)')
    -- store description
    if shortdescription then
      _comment.shortdescription = shortdescription
      -- clean description
      -- remove always the first space character
      -- (this manage the case short and long description is on the same line)
      description = string.gsub(description, "^[ \t]","")
      -- if first line is only an empty string remove it
      description = string.gsub(description, "^[ \t]*\n","")
      _comment.description = description
    else
      _comment.shortdescription = firstpart
      _comment.description = ""
    end
  end

  -- Extract tags
  -- ===================
  -- Parse regular tags
  local tag
  for i, part in ipairs(commentparts) do
    tag = parsetag(part)
    --if it's a supported tag (so tag is not nil, it's a table)
    if tag then
      if not _comment.tags then _comment.tags = {} end
      if not _comment.tags[tag.tagname] then
        _comment.tags[tag.tagname] = {}
      end
      table.insert(_comment.tags[tag.tagname], tag)
    else

      -- Try user defined tags, so far they will look like
      -- @identifier description
      local tagname, thirdtag = parsethirdtag( part )
      if tagname then
        --
        -- Append found tag
        --
        local reservedname = 'unknowntags'
        if not _comment.unknowntags then
          _comment.unknowntags = {}
        end

        -- Create specific section for parsed tag
        if not _comment.unknowntags[tagname] then
          _comment.unknowntags[tagname] = {}
        end
        -- Append to specific section
        table.insert(_comment.unknowntags[tagname], thirdtag)
      end
    end
  end
  return _comment
end


function M.parseinlinecomment(stringcomment)
  --TODO this code is use to activate typage only on --- comments. (deactivate for now)
  --  if not stringcomment or not stringcomment:find("^-") then
  --    -- if this comment don't start by -, we will not handle it.
  --    return nil
  --  end
  --  -- remove the first '-'
  --  stringcomment = string.sub(stringcomment,2)
  --  print (stringcomment)
  --  io.flush()
  local valid, parsedtag = pcall(typerefparser, lx:newstream(stringcomment, 'typeref parser'))
  if valid then
    local endoffset = parsedtag.lineinfo.last.offset
    parsedtag.description = stringcomment:sub(endoffset+2,-1)
    return parsedtag
  end
end

return M

Back to the top