Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active August 29, 2015 14:15
Show Gist options
  • Save PhilipWitte/fb32e7c63a47fb26506b to your computer and use it in GitHub Desktop.
Save PhilipWitte/fb32e7c63a47fb26506b to your computer and use it in GitHub Desktop.
import unsigned, macros
type
TokenType = enum
comment, qstr, phrase, local_dot_atom_pre_comment, local_dot_atom, local_quoted_string, domain, obs_domain_list, angle_addr, address, group_name, msg_id, date, time, received_tokens, major_digits, minor_digits, param_val, param_attr, main_type, sub_type, encoding, disp_type, ctime_date, token_string
EventType = enum
enter, exit
Event = object
tokentype: TokenType
position: int
eventtype: EventType
Token = object
tokentype: TokenType
position: Slice[int]
macro genCActions(mailstart, container): stmt =
result = newStmtList()
for token in TokenType:
let iToken = ident($token)
let sToken = ident($token & "_s")
let eToken = ident($token & "_e")
result.add quote do:
{.emit: "action `sToken` { `sToken`(p) }".}
{.emit: "action `eToken` { `eToken`(p) }".}
proc `sToken`(fpc: uint) {.exportc.} =
let position = int(fpc - mailstart)
container.add(Event(tokentype: `iToken`, position: position, eventtype: enter))
proc `eToken`(fpc: uint) {.exportc.} =
let position = int(fpc - mailstart)
container.add(Event(tokentype: `iToken`, position: position, eventtype: exit))
{.emit: """
%%{
machine common;
write init;
}%%
""" .}
proc parse*(email: cstring): seq[Event] =
var container: seq[Event] = @[]
var p {.exportc.} = cast[uint](email)
var pe {.exportc.} = uint(len(email)) + p
let mailstart = p
genCActions(mailstart, container)
{.emit: """
%%{
include common "common.rl";
write init;
write exec;
}%%
""".}
result = container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment