package diffast-langs-verilog-parsing

  1. Overview
  2. Docs
Verilog parser for Diff/AST

Install

Dune Dependency

Authors

Maintainers

Sources

v0.1.1.tar.gz
sha256=2b36318d1317efb4e3ae71727934aa8883dde49ad5dd71d85df6a9b9b10bfe0a
md5=a0d35178f15cd2d2a3b907a0bbcc959c

doc/src/diffast-langs-verilog-parsing/lib.ml.html

Source file lib.ml

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
(*
   Copyright 2012-2025 Codinuum Software Lab <https://codinuum.com>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*)
(* lib.ml *)

[%%prepare_logger]

module Storage = Diffast_misc.Storage
module Parserlib_base = Langs_common.Parserlib_base
module Astloc = Langs_common.Astloc

module Loc = Astloc
module Aux = Parser_aux
module PB = Parserlib_base
module C = Context

let mkparser = PB.mkparser


let predefined_macrotbl =
  let open Tokens_ in

  let mkstring id  = PP_MACRO_CONST_STR id in
  let mkinteger id = PP_MACRO_CONST_INT id in

  let tbl = new Macro.table "predefined" in
  let list =
    [
      "`__FILE__", [], Macro.mk_line mkstring;
      "`__LINE__", [], Macro.mk_line mkinteger;
    ]
  in
  List.iter
    (fun (id, args, mk_line) ->
      let body =
        match args with
        | [] -> Macro.Object (mk_line id)
        | _ -> Macro.Function(args, mk_line id)
      in
      tbl#define id body
    ) list;
  tbl


[%%capture_path
class parser_c = object (self)
  inherit [Source.c, Tokens_.token, Ast.c] PB.c (new Aux.env)

  val mutable context_stack = Obj.magic ()

  val mutable begin_scope = fun () -> ()
  val mutable end_scope   = fun () -> ()

  val mutable parser_partial_description_list      = fun _ -> Obj.magic ()
  val mutable parser_partial_module_item_list      = fun _ -> Obj.magic ()
  val mutable parser_partial_generate_item_list    = fun _ -> Obj.magic ()
  val mutable parser_partial_block_decl_stmt_list  = fun _ -> Obj.magic ()
  val mutable parser_partial_case_item_list        = fun _ -> Obj.magic ()
  val mutable parser_partial_case_inside_item_list = fun _ -> Obj.magic ()
  val mutable parser_partial_cellpin_list          = fun _ -> Obj.magic ()
  val mutable parser_partial_list_of_ports         = fun _ -> Obj.magic ()
  val mutable parser_partial_pev_expr              = fun _ -> Obj.magic ()
  val mutable parser_partial_ev_expr               = fun _ -> Obj.magic ()
  val mutable parser_partial_expr                  = fun _ -> Obj.magic ()

  val mutable parser_main                          = fun _ -> Ast.dummy_node


  val mutable parse_error = fun _ _ _ -> Common.fail_to_parse ""

  val mutable scanner = Obj.magic ()

  val mutable _parse = fun () -> Obj.magic ()

  method set_ignore_include_flag = env#set_ignore_include_flag
  method clear_ignore_include_flag = env#clear_ignore_include_flag

  method dump_ignored_regions =
    env#ignored_regions#dump

  method ignored_LOC =
    env#ignored_regions#get_LOC

  method dump_missed_regions =
    env#missed_regions#dump

  method missed_LOC =
    env#missed_regions#get_LOC


  method partial_parser_selector c =
    match C.get_tag c with
    | C.Cunknown               -> [%debug_log "not found"]; raise Not_found
    | C.Ctoplevel              -> parser_partial_description_list
    | C.Cmodule_item_list      -> parser_partial_module_item_list
    | C.Cgenerate_item_list    -> parser_partial_generate_item_list
    | C.Cblock_decl_stmt_list  -> parser_partial_block_decl_stmt_list
    | C.Ccase_item_list        -> parser_partial_case_item_list
    | C.Ccase_inside_item_list -> parser_partial_case_inside_item_list
    | C.Ccellpin_list          -> parser_partial_cellpin_list
    | C.Clist_of_ports         -> parser_partial_list_of_ports
    | C.Cpev_expr              -> parser_partial_pev_expr
    | C.Cev_expr               -> parser_partial_ev_expr
    | C.Cexpr                  -> parser_partial_expr

  method! parser_init =
    [%debug_log "initializing..."];
    begin_scope();
    List.iter
      (fun (pkg, _) ->
	env#import_any pkg;
	env#register_identifier pkg Aux.IApackage
      ) Aux.builtin_packages;
    context_stack#reset;
    [%debug_log "finished"]


  method _parse = _parse()

  method macrotbl = env#macrotbl
  method set_predefined_macrotbl tbl = env#set_predefined_macrotbl tbl


  method make_source file =
    new Source.c file

  method make_source_stdin =
    new Source.c Storage.stdin


  method __parse =
(*    self#parser_init; *)
    try
      let root = parser_main scanner#get_token in
      let ast = new Ast.c root in

      ast#set_lines_read (env#lines_read + env#current_pos_mgr#lines_read);
      ast#set_comment_regions env#comment_regions#get_offsets;
      ast#set_comment_LOC env#comment_regions#get_LOC;
      ast#set_missed_regions (env#missed_regions#get_offsets);
      ast#set_missed_LOC (env#missed_regions#get_LOC);
      ast#set_ignored_regions (env#ignored_regions#get_offsets);
      ast#set_ignored_LOC (env#ignored_regions#get_LOC);
      ast
    with
    | Parsing.Parse_error ->
	let l, c = env#current_pos_mgr#get_current_position in
        Common.fail_to_parse
          ~head:(Printf.sprintf "[%s:%d:%d]" env#current_filename l c)
          "syntax error"



  initializer
    context_stack <- new C.stack env;
    let module S = struct
      let env           = env
      let context_stack = context_stack
    end
    in
    let module A = Aux.F (S) in
    let module P = Parser.Make (S) in
    let module TB = Tokenbuffer.F (S) in
    let module Scan = Scanner.F (S) in

    begin_scope <- A.begin_scope;
    end_scope   <- A.end_scope;

    scanner <- new Scan.c env self#partial_parser_selector;

    parser_partial_description_list      <- mkparser P.partial_description_list;
    parser_partial_module_item_list      <- mkparser P.partial_module_item_list;
    parser_partial_generate_item_list    <- mkparser P.partial_gen_item_list;
    parser_partial_block_decl_stmt_list  <- mkparser P.partial_block_decl_stmt_list;
    parser_partial_case_item_list        <- mkparser P.partial_case_item_list;
    parser_partial_case_inside_item_list <- mkparser P.partial_case_inside_item_list;
    parser_partial_cellpin_list          <- mkparser P.partial_cellpin_list;
    parser_partial_list_of_ports         <- mkparser P.partial_list_of_ports;
    parser_partial_pev_expr              <- mkparser P.partial_pev_expr;
    parser_partial_ev_expr               <- mkparser P.partial_ev_expr;
    parser_partial_expr                  <- mkparser P.partial_expr;

    parser_main                          <- mkparser P.main;

    parse_error <- A.parse_error;

    _parse <-
      (fun () ->
	try
	  self#__parse
	with
	| P.Error ->
	    let l, c = env#current_pos_mgr#get_current_position in
            Common.fail_to_parse
              ~head:(Printf.sprintf "[%s:%d:%d]" env#current_filename l c)
              "syntax error"
      )


end (* of Lib.parser_c *)
]
OCaml

Innovation. Community. Security.