Skip to content

Demo a flag-based tooling parser #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions fluent/syntax/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
from .errors import ParseError


def bitfield():
i = 0
while True:
yield 2**i
i += 1

FLAG = bitfield()

SLASH_COMMENTS = next(FLAG)
POUND_COMMENTS = next(FLAG)
SECTIONS = next(FLAG)
TERMS = next(FLAG)


def with_span(fn):
def decorated(self, ps, *args):
if not self.with_spans:
Expand All @@ -26,8 +40,16 @@ def decorated(self, ps, *args):


class FluentParser(object):

ZERO_FOUR = SLASH_COMMENTS | SECTIONS
ZERO_FIVE = POUND_COMMENTS | TERMS

def __init__(self, with_spans=True):
self.with_spans = with_spans
self.features = None

def has_feature(self, flag):
return self.features is None or flag & self.features

def parse(self, source):
ps = FluentParserStream(source)
Expand Down Expand Up @@ -130,14 +152,23 @@ def get_entry_or_junk(self, ps):
return junk

def get_entry(self, ps):
if ps.current_char == '#':
return self.get_comment(ps)
if self.has_feature(POUND_COMMENTS) and ps.current_char == '#':
node = self.get_comment(ps)
if self.features is None:
self.features = self.ZERO_FIVE
return node

if ps.current_char == '/':
return self.get_zero_four_style_comment(ps)
if self.has_feature(SLASH_COMMENTS) and ps.current_char == '/':
node = self.get_zero_four_style_comment(ps)
if self.features is None:
self.features = self.ZERO_FOUR
return node

if ps.current_char == '[':
return self.get_group_comment_from_section(ps)
if self.has_feature(SECTIONS) and ps.current_char == '[':
node = self.get_group_comment_from_section(ps)
if self.features is None:
self.features = self.ZERO_FOUR
return node

if ps.current_char == '-':
return self.get_term(ps)
Expand Down