public static Term AttributeToTerm(ETF_TextBlock.ETF_Attribute attribute) { Term t=Term.Create_SMALL_TUPLE(); if (attribute.Name==null) t.AddSmallAtom(""); else t.AddSmallAtom(attribute.Name); switch (attribute.Type) { case TermType.ATOM: t.AddAtom(attribute.Value); break; /*case TermType.BINARY: t.AddAtom(attribute.Value); break;*/ case TermType.INTEGER: t.AddInt(int.Parse(attribute.Value)); break; case TermType.NEW_FLOAT: t.AddFloat(float.Parse(attribute.Value)); break; case TermType.NIL: t.AddNull(); break; case TermType.SMALL_ATOM: t.AddSmallAtom(attribute.Value); break; case TermType.SMALL_INTEGER: t.AddByte(byte.Parse(attribute.Value)); break; case TermType.STRING: t.AddString(attribute.Value); break; default: throw new Exception("AttributeToTerm: not supported term type="+attribute.Type.ToString()); } return t; }
public static Term BlockToTerm(ETF_TextBlock Block) { if (Block == null) return null; Term root=Term.Create_LIST(); if (Block.Name == null) root.AddSmallAtom(""); else root.AddSmallAtom(Block.Name); Term child; //attributes foreach (ETF_TextBlock.ETF_Attribute a in Block.Attributes) { root.TupleVal.Add(AttributeToTerm(a)); } if (Block.Children.Count > 0) { child = root.AddList(); foreach (ETF_TextBlock c_block in Block.Children) { child.TupleVal.Add(BlockToTerm(c_block)); } } return root; }
static void TermToThisBlock(ETF_TextBlock block,Term term) { //this is Block if (!string.IsNullOrEmpty(term.TupleVal[0].StrVal)) block.Name = term.TupleVal[0].StrVal; //other for (int i = 1; i < term.TupleVal.Count; i++) { if (term.TupleVal[i].GetTermType() == TermType.SMALL_TUPLE) { //this is Attribute //TODO : Может и стоит добавить пару лишних проверок)), но пока можно // отлаживать, это тут мы увидем, а потом нахуй) block.SetAttribute(term.TupleVal[i].TupleVal[0].StrVal, //atom Name term.TupleVal[i].TupleVal[1].ToString(), //Some value term.TupleVal[i].TupleVal[1].GetTermType());//type } else if (term.TupleVal[i].GetTermType() == TermType.LIST) { //this is childrens foreach (Term t in term.TupleVal[i].TupleVal) { ETF_TextBlock tmp = block.AddChild("t"); TermToThisBlock(tmp, t); } } else throw new Exception("TermToBlock: wait List, have" + term.TupleVal[i].GetTermType().ToString() + " at:" + i.ToString() + "\n Term: " + term.ToString()); } }
/// <summary> /// Deletes child block. /// </summary> /// <param name="child">The child block.</param> public void DeleteChild( ETF_TextBlock child ) { children.Remove( child ); child.parent = null; child.name = ""; child.data = ""; child.children = null; child.attributes = null; }
/// <summary> /// Creates the child block. /// </summary> /// <param name="name">The block name.</param> /// <param name="data">The block data string.</param> /// <returns>The child block.</returns> /// <remarks> /// Names of blocks can repeat. /// </remarks> public ETF_TextBlock AddChild( string name, string data ) { if( string.IsNullOrEmpty( name ) ) //Log.Fatal( "TextBlock.AddChild: \"name\" is null or empty." ); throw new Exception( "TextBlock.AddChild: \"name\" is null or empty." ); ETF_TextBlock child = new ETF_TextBlock(); child.parent = this; child.name = name; child.data = data; child.type=TermType.STRING; children.Add( child ); return child; }
static bool LoadChild( ETF_TextBlock child, bool ifEmptyLexReturnTrue ) { while( true ) { bool lexIntoQuotes; string lex = GetLexeme( false, out lexIntoQuotes ); if( lex.Length == 0 )//if( lex == "" ) { if( ifEmptyLexReturnTrue ) return true; Error( "Unexpected end of file" ); return false; } if( lex == "}" ) return true; string lex2 = GetLexeme( false ); if( lex2.Length == 0 )//if( lex2 == "" ) { Error( "Unexpected end of file" ); return false; } if( lex2 == "=" ) { string s_type = GetLexeme( true ); TermType t_type=ETF_TextBlock.StringToTermType(s_type,true); if (t_type==TermType.ERROR) { Error( "Invalid type description" ); return false; } if (t_type==TermType.ERLANG_BINARY) child.SetAttribute( lex, s_type,TermType.STRING ); else { string value = GetLexeme( true ); child.SetAttribute( lex, value,t_type ); } continue; } if( lex2 == "{" ) { ETF_TextBlock c = child.AddChild(lex); if( !LoadChild( c, false ) ) return false; continue; } string lex3 = GetLexeme( false ); if( lex3.Length == 0 )//if( lex3 == "" ) { Error( "Unexpected end of file" ); return false; } if( lex3 == "{" ) { ETF_TextBlock c = child.AddChild(lex, lex2); if( !LoadChild( c, false ) ) return false; continue; } Error( "Invalid file format" ); return false; } }
public static ETF_TextBlock Parse( string str, out string errorString ) { if( str == null ) //Log.Fatal( "TextBlock.Parse: \"str\" is null." ); throw new Exception( "TextBlock.Parse: \"str\" is null." ); streamString = str; streamPosition = 0; error = null; linePosition = 1; root = new ETF_TextBlock(); bool ret = LoadChild( root, true ); if( !ret ) { errorString = error; return null; } errorString = ""; return root; }