PROGRAMMES PYTHON
**module** lang = py
  notation
    ::=
      {a0}
      print ('"{a0}")
        {L}
    ::=
      {a0}
      print ("==> {a0}")
        {L}
      print ("<== {a0}")
  def parse_point_prolog_2 (S)
    # DESCRIPTION
      # coupe S à partir des ' . ' et ' ..'
      # OUTPUT
    quelques constantes
      sep_1 = ",)]}"
        sep_2 = '([{'
      S1 = ' . '
        S2 = ' .. '
    init
      tail = False
        # indique s'il y a un .. dans la chaine
    verifs rapides
      vérification rapide
        CASE
          len (S) < 5
            S
          not S.find (' .')
            S
          not S.find ('. ')
            S
    print ('=====================')
      print ('Expression =', S)
    recherche du premier ' . ' ou ' .. '
      found = False
        i = 0
          N = len (S)
            in_string = False
              par_level = 0
      while i < N
        c = S[i]
          print ('06-->', i, c, l_m, l_d, par_level)
            CASE
              in_string
                if c = sep
                  in_string = False
              c = '"' or c = "'"
                in_string = True
                  sep = c
              c = ' '
                i1 = i+1
                  if i1 >= N
                    break
                  c1 = S[i1]
                    if c1 != '.'
                      i +=1
                        continue
                  i2 = i+2
                    if i2 >= N
                      break
                  c2 = S[i2]
                    CASE
                      c2 = ' '
                        i +=2
                          found = True
                            break
                        # cas du ' . '
                          #note
                      c2 = '.'
                        i3 = i + 3
                          if i3 >= N
                            break
                        c3 = S[i3]
                          if c3 = ' '
                            i +=3
                              tail = True
                              found = True
                                break
                        # cas du ' .. '
                          #note
        i +=1
      if not found
        S
      pos = i
        pos est l'indice où apparait le premier space de ' . ' ou ' .. '
        print ( S[0:pos], '-->', S [pos:] )
    pour toute la suite
      some_error = False
      l_d = []
        liste des termes
      l_m = []
      in_par = False
        # note
        # in_par = 1 si c = '('
        # in_par = 2 si c = '['
        # in_par = 3 si c = '{'
      in_string = False
      par_level = 0
      sep = False
    recherche en arrière du premier_terme
      ou à la volée ?
      recherche arrière
        while i >= 0
          WHERE
            i = pos
              par_level = 0
          c = S [i]
            print ('07-->', i, c, par_level)
            CASE
              in_string
                if c = sep
                  in_string = False
                fin du string
                  #note
              c = '"' or c = "'"
                in_string = True
                  sep = c
                début de string
                  #note
              c = ','
                if par_level = 0
                  print (51, ' virgule', c)
                    break
              c in sep_1
                if c != ','
                  par_level += 1
                ferme parenthèse
                  #note
                  sep_1 = ",)]}"
              c in sep_2
                if par_level = 0
                  print (51, 'break sep1', c)
                    break
                par_level -=1
                nouvelle parenthèse
                  #note
                  sep_2 = '([{'
          i -= 1
      découpe S [0 : pos] en S[0:start] et S[start : pos]
        if i < 0
          before = ''
            terme_1 = S[0:pos+1].strip ()
          else
            before = S [0 : i+1]
              terme_1 = S[i+1:pos+1].strip ()
        print ('i, before --> terme_1 = ', i, before, '-->', terme_1 )
    parse de la suite
      ...
        l_m = []
          i = pos + 3
            if tail
              pos +=1
        l_d = [terme_1.strip ()]
          print ('suite :', S[i:] )
        while i < N
          c = S [i]
            print ('08-->', i, c, l_m, l_d, par_level)
              [c, c1, c2 ] = [space, point , space]
              [c, c1, c2, c3] = [space, point , point, space]
            CASE
              in_string
                l_m.append (c)
                  if c = sep
                    # fin
                    in_string = False
              c = '"' or c = "'"
                l_m.append (c)
                  in_string = True
                    sep = c
              c in sep_1
                if par_level = 0
                  print (51, 'break sep1', c)
                    break
                if c != ','
                  par_level -= 1
                    l_m.append (c)
                ferme parenthèse
                  #note
                  sep_1 = ",)]}"
              c in sep_2
                l_m.append (c)
                  par_level +=1
                nouvelle parenthèse
                  #note
                  sep_2 = '([{'
              c = ' '
                i1 = i+1
                  if i1 >= N
                    l_m.append (c)
                      break
                  c1 = S[i1]
                    if c1 != '.'
                      l_m.append (c)
                        i+=1
                          continue
                  i2 = i+2
                    if i2 >= N
                      l_m.append (c)
                        l_m.append (c1)
                          break
                  c2 = S[i2]
                    CASE
                      c2 = ' '
                        l_d.append (''.join(l_m))
                          l_m = []
                            i +=3
                              continue
                        # cas du ' . '
                          #note
                      c2 = '.'
                        i3 = i + 3
                          if i3 >= N
                            l_m.append (c)
                              l_m.append (c1)
                                break
                        c3 = S[i3]
                          if c3 = ' '
                            l_d.append (''.join(l_m))
                              l_m = []
                                i +=4
                                  tail = True
                                  continue
                        # cas du ' .. '
                          #note
                      else
                        l_m.append (c)
              else
                l_m.append (c)
                # next
          i += 1
    phase finale : préparation de infos à retourner
      tail_m = False
        if l_m != []
          if tail
            tail_m = ''.join (l_m).strip ()
            else
              l_d.append (''.join (l_m))
              tail_m = False
      l_d = [parse_point_prolog_2(x.strip ()) for x in l_d]
      if par_level > 0
        some_error = True
      if in_string
        some_error = True
      reste = S [i:]
      print ('rewriting ', S, 'as :')
      print ('\t ', before, '//', l_d, '....>>', tail_m, '//', reste, '\n |---- ', some_error)
      skip = ' <<<'
      reste = parse_point_prolog_2 (reste)
      if tail
        print (skip, before, '[', ' ,'.join(l_d) , '|', tail_m, ']', reste)
          result = before + '[' + ', '.join(l_d) + ' | ' + tail_m + ']' + reste
        else
          print (skip, before, '[', ' ,'.join(l_d) , ']', reste)
            result = before + '[' + ', '.join(l_d) + ']' + reste
      recommence jusqu'à plus soif
        result = parse_point_prolog_2 (result)
      print ( skip, result)
    result
  if __name__ = '__main__'
    # peut-être faire des choses ... mais là : rien
    # doit sauter le quote qui est une constante
      L = [
        'X . Y .. L', 'X . Y', 'X .. Y', 'X . Y .. L, haha', 'X . Y .. L), haha', 'X . Y . Z ) U', "foo (1 . 2 . 3, X2 .. L)" , "foo ('1 . 2 . 3', X2 .. L)" ]
    tests
      #tests
        L1 = [
          'X . Y'
          'X .. Y'
          'X . Y .. L'
          'X . Y .. L, haha'
          'X . Y . Z ) U'
          'X . Y .. L), haha'
          'foo (1 . 2 . 3, X2 .. L)'
          'foo (1, (1 . 2 . 3) . 4 .. 5, etc)'
        L2 = [
          1 . 2 . 3 .. L2
          1 . (2 , 3) . 3 .. L2
          1 . (2 , 3)
          1 .. (2 , 3)
          1 . [2 . 3] . 3 .. L2
          1 .. [2 . 3]
        'foo ("1 . 2 . 3", X2 .. L)'
      #tests,
        L3 = [ 'X . Y .. L', 'X . Y', 'X .. Y', ', X .. Y', 'X . Y .. L, haha', 'X . Y .. L), haha', 'X . Y . Z ) U', 'foo (1 . 2 . 3, X2 .. L)' , 'foo ("1 . 2 . 3", X2 .. L)' , 'foo (f (X . Y), Y .. L)', 'foo (1, (1 . 2 . 3) . 4 .. 5, etc)']
      print ('=====================')
        print ('==== TESTS ====')
          print ('=====================')
    for L in [L1, L3]
      for S in L
        a, b, c = parse_point_prolog (S)
          print ( '------------------------------')
          pass
        print ( '**TEST :', S, '-->')
          result = parse_point_prolog_2 (S)
            print ( ' ==>', result)