Lichen

encoders.py

25:fa9a7f9773dc
2016-09-04 Paul Boddie Minor comment reformatting.
     1 #!/usr/bin/env python     2      3 """     4 Encoder functions, producing representations of program objects.     5      6 Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 # Output encoding and decoding for the summary files.    23     24 def encode_attrnames(attrnames):    25     26     "Encode the 'attrnames' representing usage."    27     28     return ", ".join(attrnames) or "{}"    29     30 def encode_constrained(constrained):    31     32     "Encode the 'constrained' status for program summaries."    33     34     return constrained and "constrained" or "deduced"    35     36 def encode_usage(usage):    37     38     "Encode attribute details from 'usage'."    39     40     all_attrnames = []    41     for t in usage:    42         all_attrnames.append(t)    43     return ", ".join(all_attrnames) or "{}"    44     45 def encode_access_location(t):    46     47     "Encode the access location 't'."    48     49     path, name, attrname, version = t    50     return "%s %s %s:%d" % (path, name or "{}", attrname, version)    51     52 def encode_location(t):    53     54     "Encode the general location 't' in a concise form."    55     56     path, name, attrname, version = t    57     if name is not None and version is not None:    58         return "%s %s:%d" % (path, name, version)    59     elif name is not None:    60         return "%s %s" % (path, name)    61     else:    62         return "%s :%s" % (path, attrname)    63     64 def encode_modifiers(modifiers):    65     66     "Encode assignment details from 'modifiers'."    67     68     all_modifiers = []    69     for t in modifiers:    70         all_modifiers.append(encode_modifier_term(t))    71     return "".join(all_modifiers)    72     73 def encode_modifier_term(t):    74     75     "Encode modifier 't' representing assignment status."    76     77     assignment = t    78     return assignment and "A" or "_"    79     80 def decode_modifier_term(s):    81     82     "Decode modifier term 's' representing assignment status."    83     84     return s == "A"    85     86 # Output program encoding.    87     88 def encode_function_pointer(path):    89     90     "Encode 'path' as a reference to an output program function."    91     92     return "__fn_%s" % encode_path(path)    93     94 def encode_instantiator_pointer(path):    95     96     "Encode 'path' as a reference to an output program instantiator."    97     98     return "__new_%s" % encode_path(path)    99    100 def encode_path(path):   101    102     "Encode 'path' as an output program object, translating special symbols."   103    104     if path in reserved_words:   105         return "__%s" % path   106     else:   107         return path.replace("#", "__").replace("$", "__").replace(".", "_")   108    109 def encode_symbol(symbol_type, path=None):   110    111     "Encode a symbol with the given 'symbol_type' and optional 'path'."   112    113     return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "")   114    115 # Output language reserved words.   116    117 reserved_words = [   118     "break", "char", "const", "continue",   119     "default", "double", "else",   120     "float", "for",   121     "if", "int", "long",   122     "NULL",   123     "return", "struct",   124     "typedef",   125     "void", "while",   126     ]   127    128 # vim: tabstop=4 expandtab shiftwidth=4