Example #1
0
        internal Object Unmarshal(HParsedToken tok)
        {
            // TODO
            switch (tok.token_type)
            {
            case HTokenType.TT_NONE:
                return(null);

            case HTokenType.TT_BYTES:
            {
                byte[] ret = new byte[tok.token_data.bytes.len];
                Marshal.Copy(tok.token_data.bytes.token,
                             ret,
                             0, ret.Length);
                return(ret);
            }

            case HTokenType.TT_SINT:
                return((System.Int64)tok.token_data.sint);

            case HTokenType.TT_UINT:
                return((System.UInt64)tok.token_data._uint);

            case HTokenType.TT_SEQUENCE:
            {
                Object[] ret = new Object[tok.token_data.seq.used];
                for (uint i = 0; i < ret.Length; i++)
                {
                    ret[i] = Unmarshal(tok.token_data.seq.at(i));
                }
                return(ret);
            }

            default:
                if (tok.token_type == Hammer.tt_dotnet)
                {
                    HTaggedToken tagged       = hammer.h_parsed_token_get_tagged_token(tok);
                    Object       cb           = Hammer.tag_to_action[tagged.label];
                    Object       unmarshalled = Unmarshal(tagged.token);
                    if (cb is HAction)
                    {
                        HAction act = (HAction)cb;
                        return(act(unmarshalled));
                    }
                    else if (cb is HPredicate)
                    {
                        HPredicate pred = (HPredicate)cb;
                        if (!pred(unmarshalled))
                        {
                            throw new ParseError("Predicate failed");
                        }
                        else
                        {
                            return(unmarshalled);
                        }
                    }
                }
                throw new Exception("Should not reach here");
            }
        }
Example #2
0
 internal Object Unmarshal(HParsedToken tok)
 {
     // TODO
       switch(tok.token_type) {
       case HTokenType.TT_NONE:
     return null;
       case HTokenType.TT_BYTES:
     {
       byte[] ret = new byte[tok.token_data.bytes.len];
       Marshal.Copy(tok.token_data.bytes.token,
                ret,
                0, ret.Length);
       return ret;
     }
       case HTokenType.TT_SINT:
     return (System.Int64)tok.token_data.sint;
       case HTokenType.TT_UINT:
     return (System.UInt64)tok.token_data._uint;
       case HTokenType.TT_SEQUENCE:
     {
       Object[] ret = new Object[tok.token_data.seq.used];
       for (uint i = 0; i < ret.Length; i++)
     ret[i] = Unmarshal(tok.token_data.seq.at(i));
       return ret;
     }
       default:
     if (tok.token_type == Hammer.tt_dotnet)
       {
     HTaggedToken tagged = hammer.h_parsed_token_get_tagged_token(tok);
     Object cb = Hammer.tag_to_action[tagged.label];
     Object unmarshalled = Unmarshal(tagged.token);
     if (cb is HAction) {
       HAction act = (HAction)cb;
       return act(unmarshalled);
     } else if (cb is HPredicate) {
       HPredicate pred = (HPredicate)cb;
       if (!pred(unmarshalled))
         throw new ParseError("Predicate failed");
       else
         return unmarshalled;
     }
       }
     throw new Exception("Should not reach here");
       }
 }