source: rtems-central/formal/promela/src/src/library.coco @ 1283c16

Last change on this file since 1283c16 was 1283c16, checked in by Andrew Butterfield <Andrew.Butterfield@…>, on 01/10/23 at 19:57:22

main Coconut sources

  • Property mode set to 100644
File size: 3.1 KB
Line 
1 ##############################################################################
2 # Library
3 #
4 # Copyright (C) 2019-2021 Trinity College Dublin (www.tcd.ie)
5 #
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met:
11 #
12 #     * Redistributions of source code must retain the above copyright
13 #       notice, this list of conditions and the following disclaimer.
14 #
15 #     * Redistributions in binary form must reproduce the above
16 #       copyright notice, this list of conditions and the following
17 #       disclaimer in the documentation and/or other materials provided
18 #       with the distribution.
19 #
20 #     * Neither the name of the copyright holders nor the names of its
21 #       contributors may be used to endorse or promote products derived
22 #       from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ##############################################################################
36
37import fileinput
38import ply.yacc
39import sys
40
41#
42
43def input0 ():
44    input ('Continue? (ENTER/CTRL+C): ')
45
46def input_continue (msg):
47    print msg
48    input0 ()
49
50input_continue_logger = ply.yacc.PlyLogger (sys.stderr)
51
52def input_continue_warn (msg):
53    input_continue_logger.warning (msg)
54    input0 ()
55
56def input_continue_err (msg):
57    input_continue_logger.error (msg)
58    input0 ()
59
60#
61
62def isinst (ty, f_get, f_check, toks):
63    return isinstance (toks, ty) and all ([ f_check tok for tok in f_get toks ])
64def isinst_list (f_check, toks):
65    return isinst (list, (toks -> toks), f_check, toks)
66def isinst_dict (check1_check2, toks):
67    return isinst (dict, (toks -> toks.items ()), (toks -> check1_check2[0] (toks[0]) and check1_check2[1] (toks[1])), toks)
68def isinst_str (toks):
69    return isinstance (toks, str)
70
71#
72
73def mapi (f, l):
74    n = 0
75    for x in l:
76        yield f ((n, x))
77        n += 1
78
79def flatten (l): return (x for xs in l for x in xs)
80
81def update_dic (dic, pos, cmds):
82    dic[pos] = dic[pos] + cmds if pos in dic else cmds
83
84def fileinput_input (path):
85    try:
86        with fileinput.input path as lines:
87            for line in lines:
88                yield line
89    except FileNotFoundError:
90        input_continue (f'File not found: {path}')
91    except IsADirectoryError:
92        input_continue (f'Is a directory: {path}')
93
94fileinput_input0 = ''.join <.. fileinput_input
Note: See TracBrowser for help on using the repository browser.