66 Fortran type-declaration statements. 81 "intrinsic_type_spec",
82 "declaration_type_spec",
109 <declaration-type-spec> [ [, <attr-spec>] :: ] <entity-decl-list> 110 <declaration-type-spec> = <intrinsic-type-spec> 111 | TYPE ( <derived-type-spec> ) 112 | CLASS ( <derived-type-spec> ) 115 <derived-type-spec> = <type-name> [ ( <type-param-spec-list> ) ] 116 <type-param-spec> = [ <keyword> = ] <type-param-value> 117 <type-param-value> = <scalar-int-expr> | * | : 119 <intrinsic-type-spec> = INTEGER [<kind-selector>] 120 | REAL [<kind-selector>] 122 | COMPLEX [<kind-selector>] 123 | CHARACTER [<char-selector>] 124 | LOGICAL [<kind-selector>] 126 <kind-selector> = ( [ KIND = ] <scalar-int-initialization-expr> ) 128 <kind-selector> = ( [ KIND = ] <scalar-int-initialization-expr> ) 131 <char-selector> = <length-selector> 132 | ( LEN = <type-param-value>, KIND = <scalar-int-initialization-expr> ) 133 | ( <type-param-value>, [ KIND = ] <scalar-int-initialization-expr> ) 134 | ( KIND = <scalar-int-initialization-expr> [, LEN = <type-param-value>] ) 135 <length-selector> = ( [ LEN = ] <type-param-value> ) 136 | * <char-length> [ , ] 137 <char-length> = ( <type-param-value> ) | <scalar-int-literal-constant> 139 <attr-spec> = <access-spec> | ALLOCATABLE | ASYNCHRONOUS 140 | DIMENSION ( <array-spec> ) | EXTERNAL 141 | INTENT ( <intent-spec> ) | INTRINSIC 142 | <language-binding-spec> | OPTIONAL 143 | PARAMETER | POINTER | PROTECTED | SAVE 144 | TARGET | VALUE | VOLATILE 145 <entity-decl> = <object-name> [ ( <array-spec> ) ] [ * <char-length> ] [ <initialization> ] 146 | <function-name> [ * <char-length> ] 147 <initialization> = = <initialization-expr> 149 <access-spec> = PUBLIC | PRIVATE 150 <language-binding-spec> = BIND ( C [ , NAME = <scalar-char-initialization-expr>] ) 151 <array-spec> = <explicit-shape-spec-list> 152 | <assumed-shape-spec-list> 153 | <deferred-shape-spec-list> 154 | <assumed-size-spec> 155 <explicit-shape-spec> = [ <lower-bound> : ] <upper-bound> 156 <assumed-shape-spec> = [ <lower-bound> ] : 157 <deferred-shape-spec> = : 158 <assumed-size-spec> = [ <explicit-shape-spec-list> , ] [ <lower-bound> : ] * 159 <bound> = <specification-expr> 161 <int-literal-constant> = <digit-string> [ _ <kind-param> ] 162 <digit-string> = <digit> [ <digit> ].. 163 <kind-param> = <digit-string> | <scalar-int-constant-name> 170 ] + Statement._repr_attr_names
172 def process_item(self):
174 apply_map = item.apply_map
175 clsname = self.__class__.__name__.lower()
176 line = item.get_line()
177 from .block_statements
import Function
179 if not line.lower().startswith(clsname):
187 if j == len(clsname):
189 line = line[:i].replace(
" ",
"") + line[i:]
191 assert line.lower().startswith(clsname), repr((line, clsname))
192 line = line[len(clsname) :].lstrip()
194 if line.startswith(
"("):
196 selector = apply_map(line[: i + 1].strip())
197 line = line[i + 1 :].lstrip()
198 elif line.startswith(
"*"):
200 line = line[1:].lstrip()
201 if line.startswith(
"("):
203 selector += apply_map(line[: i + 1].rstrip())
204 line = line[i + 1 :].lstrip()
206 m = re.match(
r"\d+(_\w+|)|[*]", line)
211 selector += line[:i].rstrip()
212 line = line[i:].lstrip()
216 fm = Function.match(line)
218 l2 = line[: fm.end()]
219 m2 = re.match(
r".*?\b(?P<name>\w+)\Z", l2)
223 fname = m2.group(
"name")
224 fitem = item.copy(clsname + selector +
" :: " + fname, apply_map=
True)
225 self.parent.put_item(fitem)
230 if line.startswith(
","):
231 line = line[1:].lstrip()
234 if isinstance(self, Character):
244 self.
attrspec = split_comma(line[:i].rstrip(), self.item)
245 self.
entity_decls = split_comma(line[i + 2 :].lstrip(), self.item)
247 if not is_entity_decl(entity):
251 if isinstance(self.parent, Function)
and self.parent.name
in self.
entity_decls:
252 assert self.parent.typedecl
is None, repr(self.parent.typedecl)
253 self.parent.typedecl = self
255 if isinstance(self, Type):
257 assert is_name(self.
name), repr(self.
name)
262 def _parse_kind_selector(self, selector):
265 length, kind =
"",
"" 266 if selector.startswith(
"*"):
267 length = selector[1:].lstrip()
269 assert selector[0] + selector[-1] ==
"()", repr(selector)
270 l = selector[1:-1].strip()
271 if l.lower().startswith(
"kind"):
273 if l[0] + l[-1] ==
"()":
276 assert l.startswith(
"="), repr(l)
277 kind = l[1:].lstrip()
282 def _split_char_selector(self, line):
283 """line=``[key=]value`` -> key, value. 284 If line does not have name part then return None, value. 286 for name
in [
"len",
"kind"]:
287 if line[: len(name)].lower() == name:
288 value_part = line[len(name) :].lstrip()
289 if value_part.startswith(
"="):
290 return name, value_part[1:].lstrip()
293 def _parse_char_selector(self, selector):
296 if selector.startswith(
"*"):
297 l = selector[1:].lstrip()
298 if l.startswith(
"("):
301 assert l.endswith(
")"), repr(l)
303 if l.lower().startswith(
"len"):
304 l = l[3:].lstrip()[1:].lstrip()
307 assert selector[0] + selector[-1] ==
"()", repr(selector)
308 l = split_comma(selector[1:-1].strip(), self.item)
319 assert len(l) == 2, repr(l)
323 assert key1
in [
None,
"kind"], repr(key1)
324 l, kind = value0, value1
326 assert key1 ==
"len", repr(key1)
327 l, kind = value1, value0
329 assert key0
is None, repr(key0)
330 assert key1
in [
None,
"kind"], repr(key1)
331 l, kind = value0, value1
335 """Create a text representation of this object and return it""" 336 clsname = self.__class__.__name__.upper()
339 if isinstance(self, Character):
341 text +=
"(LEN=%s, KIND=%s)" % (length, kind)
343 text +=
"(LEN=%s)" % (length)
345 text +=
"(KIND=%s)" % (kind)
346 elif isinstance(self, Type):
347 text +=
"(%s)" % (kind)
348 elif isinstance(self, Class):
353 text +=
"({0})".format(kind)
356 text +=
"*%s" % (length)
358 text +=
"(KIND=%s)" % (kind)
360 return clsname + text
362 def tofortran(self, isfix=None):
363 tab = self.get_indent_tab(isfix=isfix)
366 s +=
", " +
", ".join(self.
attrspec)
378 def __eq__(self, other):
379 if self.__class__
is not other.__class__:
381 return self.
selector == other.selector
383 def astypedecl(self):
385 return self.__class__(self.parent, self.item.copy(self.
tostr()))
391 variables = self.parent.a.variables
394 access_spec_lst = [a
for a
in attrspec
if a.lower()
in [
"private",
"public"]]
396 access_spec = access_spec_lst[0]
397 attrspec.remove(access_spec)
401 name, array_spec, char_length, value = self.
_parse_entity(item)
402 var = self.parent.get_variable(name)
405 var.set_length(char_length)
406 var.set_type(typedecl)
409 var.set_bounds(array_spec)
412 if access_spec
is not None:
413 l = getattr(self.parent.a, access_spec.lower() +
"_id_list")
418 def _parse_entity(self, line):
420 assert m, repr((line, self.item, self.__class__.__name__))
421 name = line[: m.end()]
422 line = line[m.end() :].lstrip()
427 item = self.item.copy(line)
428 line = item.get_line()
429 if line.startswith(
"("):
431 assert i != -1, repr(line)
432 array_spec = parse_array_spec(line[1:i].strip(), item)
433 line = line[i + 1 :].lstrip()
435 if line.startswith(
"*"):
438 char_length = item.apply_map(line[1:].lstrip())
441 char_length = item.apply_map(line[1:i].strip())
443 if line.startswith(
"="):
444 value = item.apply_map(line[1:].lstrip())
445 return name, array_spec, char_length, value
447 def get_zero_value(self):
448 raise NotImplementedError(repr(self.__class__.__name__))
450 def assign_expression(self, name, value):
451 return "%s = %s" % (name, value)
454 return self.
selector[1]
or self.default_kind
456 def get_length(self):
459 def get_byte_size(self):
465 return self.default_kind
467 def is_intrinsic(self):
468 return not isinstance(self, (Type, Class))
470 def is_derived(self):
471 return isinstance(self, Type)
473 def is_numeric(self):
475 self, (Integer, Real, DoublePrecision, Complex, DoubleComplex, Byte)
478 def is_nonnumeric(self):
479 return isinstance(self, (Character, Logical))
483 match = re.compile(
r"integer\b", re.I).match
486 def get_zero_value(self):
490 return "0_%s" % (kind)
494 match = re.compile(
r"real\b", re.I).match
497 def get_zero_value(self):
501 return "0_%s" % (kind)
505 match = re.compile(
r"double\s*precision\b", re.I).match
508 def get_byte_size(self):
511 def get_zero_value(self):
516 match = re.compile(
r"complex\b", re.I).match
519 def get_byte_size(self):
527 def get_zero_value(self):
531 return "(0.0_%s, 0.0_%s)" % (kind, kind)
533 def get_part_typedecl(self):
535 return Real(self.parent, self.item.copy(
"REAL*%s" % (bz)))
540 match = re.compile(
r"double\s*complex\b", re.I).match
543 def get_byte_size(self):
546 def get_zero_value(self):
547 return "(0.0D0,0.0D0)" 551 match = re.compile(
r"logical\b", re.I).match
554 def get_zero_value(self):
559 match = re.compile(
r"character\b", re.I).match
562 def get_zero_value(self):
568 match = re.compile(
r"byte\b", re.I).match
571 def get_zero_value(self):
576 match = re.compile(
r"type\s*\(", re.I).match
578 def get_zero_value(self):
579 type_decl = self.get_type_decl(self.
name)
580 component_names = type_decl.a.component_names
581 components = type_decl.a.components
583 for name
in component_names:
584 var = components[name]
585 l.append(var.typedecl.get_zero_value())
586 return "%s(%s)" % (type_decl.name,
", ".join(l))
590 raise NotImplementedError(repr(self.__class__.__name__))
597 match = re.compile(
r"class\s*\(", re.I).match
602 IMPLICIT <implicit-spec-list> 604 <implicit-spec> = <declaration-type-spec> ( <letter-spec-list> ) 605 <letter-spec> = <letter> [ - <letter> ] 608 match = re.compile(
r"implicit\b", re.I).match
610 letters = string.ascii_lowercase
612 def process_item(self):
613 line = self.item.get_line()[8:].lstrip()
614 if line.lower() ==
"none":
618 for item
in split_comma(line, self.item):
620 assert i != -1
and item.endswith(
")"), repr(item)
622 for spec
in split_comma(item[i + 1 : -1].strip(), self.item):
624 s, e = spec.lower().split(
"-")
629 e = s = spec.lower().strip()
630 assert s
in self.
letters, repr((s, e))
632 tspec = item[:i].rstrip()
634 for cls
in declaration_type_spec:
636 stmt = cls(self, self.item.copy(tspec))
639 assert stmt
is not None, repr((item, line))
640 items.append((stmt, specs))
644 def tofortran(self, isfix=None):
645 tab = self.get_indent_tab(isfix=isfix)
647 return tab +
"IMPLICIT NONE" 649 for stmt, specs
in self.
items:
655 l1.append(s +
"-" + e)
656 l.append(
"%s ( %s )" % (stmt.tostr(),
", ".join(l1)))
657 return tab +
"IMPLICIT " +
", ".join(l)
661 Analyze the Implicit statments constructed by the parser and 662 set-up the associated implicit_rules belonging to the parent 663 of this object in the AST. 665 implicit_rules = self.parent.a.implicit_rules
669 "overriding previously set implicit rule mapping" 670 " %r." % (implicit_rules)
672 self.parent.a.implicit_rules =
None 674 if implicit_rules
is None:
675 self.warning(
"overriding previously set IMPLICIT NONE")
676 self.parent.a.implicit_rules = implicit_rules = {}
677 for stmt, specs
in self.
items:
678 for start, end
in specs:
679 start_idx = string.ascii_lowercase.index(start.lower())
680 end_idx = string.ascii_lowercase.index(end.lower())
681 for lchar
in string.ascii_lowercase[start_idx : end_idx + 1]:
682 implicit_rules[lchar] = stmt
686 intrinsic_type_spec = [
696 declaration_type_spec = intrinsic_type_spec + [TypeStmt, Class]
def _split_char_selector(self, line)
def _parse_char_selector(self, selector)
def tofortran(self, isfix=None)
def _parse_entity(self, line)
def _parse_kind_selector(self, selector)