Skip to content

Instantly share code, notes, and snippets.

View fabiommendes's full-sized avatar

Fábio Macêdo Mendes fabiommendes

View GitHub Profile
rec = lambda f: lambda x: f(f, x)
fat = rec(lambda f, x: 1 if x <= 1 else x * f(f, x - 1))
print('hello world')
@fabiommendes
fabiommendes / combinadores.py
Created February 19, 2021 14:39
Combinadores de parsers - Exemplo com JSON
om typing import Tuple, Callable, Any, List
ST = Tuple[int, str]
Parser = Callable[[ST], Tuple[ST, Any]]
def error(st, msg):
return SyntaxError(msg)
src = ""
pos = 0
def loads(text: str) -> object:
"""
Carrega um documento JSON e retorna o valor Python correspondente.
"""
global src, pos
# -*- coding: utf-8 -*-
# -- Sheet --
from time import sleep
DEV_MODE = 0
if DEV_MODE:
CARACTERES_DT = 0.0
LINHAS_DT = 0.05
class Reader:
def __init__(self, src, pos=0):
self.src = src.strip()
self.pos = pos
def read(self, st):
"""
Lê sub-string "st" na posição atual.
"""
if not self.src.startswith(st, self.pos):
cabala = """
a - 1
b - 2
c - 3
d - 4
e - 5
f - 6
g - 7
h - 8
i - 9
from typing import Tuple, Callable, Any, List
ST = Tuple[int, str]
Parser = Callable[[ST], Tuple[ST, Any]]
def error(st, msg):
return SyntaxError(msg)
from typing import Tuple, Callable, Any, List, Union
ST = Tuple[int, str]
AnyParser = Union["Parser", str]
class Parser:
"""
Objeto responsável por ler um padrão de texto com uma chamada ao método
Parser.parse().