def glsl_parse_tokenized(source): """Parse tokenized source.""" # End of input. if not source: return [] # Try default parses for global scope. (block, remaining) = glsl_parse_inout(source) if block: return [block] + glsl_parse_tokenized(remaining) (block, remaining) = glsl_parse_struct(source) if block: return [block] + glsl_parse_tokenized(remaining) (block, remaining) = glsl_parse_pervertex(source) if block: return [block] + glsl_parse_tokenized(remaining) (block, remaining) = glsl_parse_uniform(source) if block: return [block] + glsl_parse_tokenized(remaining) (block, remaining) = glsl_parse_function(source) if block: return [block] + glsl_parse_tokenized(remaining) # Try parses normally for local scope. (block, remaining) = glsl_parse_declaration(source) if block: return [block] + glsl_parse_tokenized(remaining) (block, remaining) = glsl_parse_assignment(source) if block: return [block] + glsl_parse_tokenized(remaining) # Fallback, should never happen. return glsl_parse_default(source)
def glsl_parse_parameter(source): """Parse parameter block.""" (inout, typeid, content) = extract_tokens(source, ("?o", "?t")) if not inout: (typeid, content) = extract_tokens(source, ("?t")) if not typeid: return (None, source) (assignment, remaining) = glsl_parse_assignment(content, False) if not assignment: raise RuntimeError("could not parse assignment from '%s'" % (str(map(str, content)))) if inout and (not inout.format(False) in ("in", "inout", "out")): raise RuntimeError("invalid inout directive for parameter: '%s'" % (inout.format(False))) return (GlslBlockParameter(inout, typeid, assignment), remaining)
def glsl_parse_scope(source, explicit=True): """Parse scope block.""" (content, remaining) = extract_tokens(source, ("?{",)) if not (content is None): return (GlslBlockScope(glsl_parse_content(content), explicit), remaining) # If explicit scope is not expected, try legal one-statement scopes. elif not explicit: (block, remaining) = glsl_parse_flow(source) if block: return (GlslBlockScope([block], explicit), remaining) (block, remaining) = glsl_parse_unary(source) if block: return (GlslBlockScope([block], explicit), remaining) (block, remaining) = glsl_parse_assignment(source) if block: return (GlslBlockScope([block], explicit), remaining) # No scope found. return (None, source)
def glsl_parse_declaration(source): """Parse declaration block.""" (typeid, content) = extract_tokens(source, ("?t", )) if not typeid: return (None, source) # Loop until nothing found. lst = [] while True: (assignment, remaining) = glsl_parse_assignment(content) if assignment: lst += [assignment] # Might have been last assignement. if assignment.getTerminator() == ";": return (GlslBlockDeclaration(typeid, lst), remaining) # Otherwise keep going. content = remaining continue # Unknown element, not a valid declaration. return (None, source)
def glsl_parse_scope(source, explicit=True): """Parse scope block.""" (content, remaining) = extract_tokens(source, ("?{", )) if not (content is None): return (GlslBlockScope(glsl_parse_content(content), explicit), remaining) # If explicit scope is not expected, try legal one-statement scopes. elif not explicit: (block, remaining) = glsl_parse_flow(source) if block: return (GlslBlockScope([block], explicit), remaining) (block, remaining) = glsl_parse_unary(source) if block: return (GlslBlockScope([block], explicit), remaining) (block, remaining) = glsl_parse_assignment(source) if block: return (GlslBlockScope([block], explicit), remaining) # No scope found. return (None, source)
def glsl_parse_declaration(source): """Parse declaration block.""" (typeid, content) = extract_tokens(source, ("?t",)) if not typeid: return (None, source) # Loop until nothing found. lst = [] while True: (assignment, remaining) = glsl_parse_assignment(content) if assignment: lst += [assignment] # Might have been last assignement. if assignment.getTerminator() == ";": return (GlslBlockDeclaration(typeid, lst), remaining) # Otherwise keep going. content = remaining continue # Unknown element, not a valid declaration. return (None, source)