package diffast-langs-fortran-parsing
Source file common.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 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(* Copyright 2013-2018 RIKEN Copyright 2018-2025 Chiba Institude of Technology 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. *) (* Author: Masatomo Hashimoto <m.hashimoto@stair.center> *) (* common.ml *) [%%prepare_logger] module Xqueue = Diffast_misc.Xqueue module Xset = Diffast_misc.Xset module Xlist = Diffast_misc.Xlist module Xoption = Diffast_misc.Xoption module Xprint = Diffast_misc.Xprint module PB = Langs_common.Parserlib_base module Loc = Langs_common.Astloc module DirectiveLine = struct type tag = | OCL | OMP | ACC | XLF | DEC type raw = { tag : tag; head : string; line : string; queue : Obj.t Xqueue.c; fixed_cont : bool; free_cont : bool; } let tag_to_string = function | OCL -> "OCL" | OMP -> "OMP" | ACC -> "ACC" | XLF -> "XLF" | DEC -> "DEC" let dummy_queue = new Xqueue.c let mkomp line q fixed_cont free_cont = { tag = OMP; head = "!$omp"; line = line; queue = q; fixed_cont = fixed_cont; free_cont = free_cont; } let mkacc line q fixed_cont free_cont = { tag = ACC; head = "!$acc"; line = line; queue = q; fixed_cont = fixed_cont; free_cont = free_cont; } let mkocl line = { tag = OCL; head = "!OCL"; line = line; queue = dummy_queue; fixed_cont = false; free_cont = false; } let mkdec prefix line = { tag = DEC; head = prefix; line = line; queue = dummy_queue; fixed_cont = false; free_cont = false; } let mkxlf trigger line q fixed_cont free_cont = { tag = XLF; head = trigger; line = line; queue = q; fixed_cont = fixed_cont; free_cont = free_cont; } end let extensions = [".f";".for";".f90";".f95"] module LangSpec = struct type t = F77 | F90 | F95 | F2003 | F2008 let to_string = function | F77 -> "F77(ISO1539:1980)" | F90 -> "F90(ISO/IEC1539:1991)" | F95 -> "F95(ISO/IEC1539-1:1997)" | F2003 -> "F2003(ISO/IEC1539-1:2004)" | F2008 -> "F2008(ISO/IEC1539-1:2010)" end module LangExtension = struct type t = IBM | Intel | PGI | PGI_CUDA | Fujitsu | Apollo let to_string = function | Fujitsu -> "Fujitsu" | IBM -> "IBM XL Fortran" | Intel -> "DEC/Compaq/Intel Fortran" | PGI -> "PGI Fortran" | PGI_CUDA -> "PGI CUDA Fortran" | Apollo -> "Apollo/Domain Fortran" class set = object val exts = Xset.create 0 method add e = Xset.add exts e method to_string = Xlist.to_string (fun s -> "["^s^"]") "" (List.map to_string (Xset.to_list exts)) end end module SourceForm = struct type t = Unknown | Fixed | Free | Mixed let to_string = function | Unknown -> "Unknown" | Fixed -> "Fixed" | Free -> "Free" | Mixed -> "Mixed" end [%%capture_path module LangConfig = struct let default_max_line_length_fixed = 72 let default_max_line_length_free = 132 class c = object (self) val mutable spec = LangSpec.F95 val exts = new LangExtension.set val mutable source_form = SourceForm.Free val mutable max_line_length = default_max_line_length_fixed val mutable max_line_length_fixed = default_max_line_length_fixed val mutable max_line_length_free = default_max_line_length_free val mutable parse_d_lines_flag = false method parse_d_lines = parse_d_lines_flag method _set_parse_d_lines_flag b = parse_d_lines_flag <- b method set_parse_d_lines_flag = parse_d_lines_flag <- true method clear_parse_d_lines_flag = parse_d_lines_flag <- false method spec = spec method set_spec s = spec <- s method set_spec_F77 = spec <- LangSpec.F77 method set_spec_F90 = spec <- LangSpec.F90 method set_spec_F95 = spec <- LangSpec.F95 method set_spec_F2003 = spec <- LangSpec.F2003 method set_spec_F2008 = spec <- LangSpec.F2008 method exts = exts method add_ext_Fujitsu = exts#add LangExtension.Fujitsu method add_ext_IBM = exts#add LangExtension.IBM method add_ext_Intel = exts#add LangExtension.Intel method add_ext_PGI = exts#add LangExtension.PGI method add_ext_PGI_CUDA = exts#add LangExtension.PGI_CUDA method add_ext_Apollo = exts#add LangExtension.Apollo method source_form = source_form method set_source_form sf = source_form <- sf; match sf with | SourceForm.Fixed -> self#set_max_line_length__fixed | SourceForm.Free -> self#set_max_line_length__free | _ -> () method set_source_form_fixed = self#set_source_form SourceForm.Fixed method set_source_form_free = self#set_source_form SourceForm.Free method is_fixed_source_form = let b = source_form = SourceForm.Fixed in [%debug_log "%B" b]; b method is_free_source_form = let b = source_form = SourceForm.Free in [%debug_log "%B" b]; b method max_line_length = max_line_length method _set_max_line_length n = [%debug_log "length=%d" n]; max_line_length <- n method set_max_line_length n = self#_set_max_line_length n; max_line_length_fixed <- n; max_line_length_free <- n method set_max_line_length_fixed n = if max_line_length_fixed < n then begin [%debug_log "%d -> %d" max_line_length_fixed n]; max_line_length_fixed <- n end method set_max_line_length_free n = if max_line_length_free < n then begin [%debug_log "%d -> %d" max_line_length_free n]; max_line_length_free <- n end method set_max_line_length__fixed = self#_set_max_line_length max_line_length_fixed method set_max_line_length__free = self#_set_max_line_length max_line_length_free method conf_F77 = self#set_spec_F77; self#set_source_form_fixed; self#set_max_line_length_fixed method conf_F90_free = self#set_spec_F90; self#set_source_form_free; self#set_max_line_length__free method conf_F90_fixed = self#set_spec_F90; self#set_source_form_fixed; self#set_max_line_length__fixed method conf_F95_free = self#set_spec_F95; self#set_source_form_free; self#set_max_line_length__free method conf_F95_fixed = self#set_spec_F95; self#set_source_form_fixed; self#set_max_line_length__fixed end end (* module Lang_config *) ] exception Undefined type name = string type label = string type var = string let parse_warning_loc loc = PB.parse_warning_loc ~head:"[Fortran]" loc let parse_warning spos epos = PB.parse_warning ~head:"[Fortran]" spos epos exception Parse_error = PB.Parse_error exception Internal_error of string let fail_to_parse = PB.fail_to_parse let string_opt_to_string ?(prefix="") ?(suffix="") s_opt = Xoption.to_string (fun s -> s) ~prefix ~suffix s_opt let int_opt_to_string ?(prefix="") ?(suffix="") s_opt = Xoption.to_string string_of_int ~prefix ~suffix s_opt let opt_to_string = Xoption.to_string let opt_to_list = Xoption.to_list let opt_to_list_map f o = try Xoption.to_list_map f o with Not_found -> [] let list_opt_to_list = Xoption.list_opt_to_list let opt_list_to_list = Xoption.list_to_list let map_opt f o = try Xoption.map f o with Not_found -> None let string_list_to_string ?(prefix=" ") sep ss = match ss with | [] -> "" | _ -> prefix^(Xlist.to_string (fun x -> x) sep ss) let int_list_to_string ?(prefix=" ") sep il = match il with | [] -> "" | _ -> prefix^(Xlist.to_string string_of_int sep il) let num_to_ordinal n = let suffix = match n mod 10 with | 1 -> "st" | 2 -> "nd" | 3 -> "rd" | _ -> "th" in Printf.sprintf "%d%s" n suffix let warning_msg = Xprint.warning ~head:"[Fortran]"