Exemplo n.º 1
0
        private static void verifyContinueOnEmptyElements(IFhirReader xfr)
        {
            Assert.AreEqual("x", xfr.CurrentElementName);
            xfr.EnterElement();

            Assert.IsTrue(ParserUtils.IsAtFhirElement(xfr));
            Assert.AreEqual("someElem", xfr.CurrentElementName);
            xfr.EnterElement();
            Assert.IsTrue(xfr.HasMoreElements());
            xfr.SkipSubElementsFor("someElem");
            xfr.LeaveElement();

            Assert.IsTrue(ParserUtils.IsAtFhirElement(xfr));
            Assert.AreEqual("someElem2", xfr.CurrentElementName);
            xfr.EnterElement();
            Assert.IsFalse(xfr.HasMoreElements());
            xfr.LeaveElement();

            xfr.LeaveElement();
        }
        public TypedMarkupData Parse(string registeredPrefix, bool isArray, string value, ITemplate template)
        {
            if (isArray)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("new bool[] {");
                bool[] bools;
                if (value.Trim() != "")
                {
                    string[] split = value.Split('|');
                    bools = new bool[split.Length];
                    for (int i = 0; i < split.Length; i++)
                    {
                        int x = ParseSingle(split[i]);
                        if (x == -1)
                        {
                            throw ParserUtils.TemplateErrorException(ParserUtils.MakeTypedMarkupErrorMessage(registeredPrefix, isArray, value));
                        }
                        bools[i] = (x == 1);
                        sb.Append(i > 0 ? ", " : " ");
                        sb.Append(x == 1 ? "true" : "false");
                    }
                }
                else
                {
                    bools = new bool[0];
                }

                sb.Append(" }");
                return(new TypedMarkupData(sb.ToString(), delegate() { return bools; }));
            }
            else
            {
                int x = ParseSingle(value);
                if (x == -1)
                {
                    throw ParserUtils.TemplateErrorException(ParserUtils.MakeTypedMarkupErrorMessage(registeredPrefix, isArray, value));
                }
                return(new TypedMarkupData(x == 1 ? "true" : "false", delegate { return x == 1; }));
            }
        }
        private void Bad_MiLi()
        {
            //This is really hard.

            //mi li anpa li pakala kin.
            Dialect     c  = Dialect.LooseyGoosey;
            ParserUtils pu = new ParserUtils(c);

            const string test = "mi li anpa li pakala kin.";

            try
            {
                Sentence s = pu.ParsedSentenceFactory(test, test);
                Console.WriteLine(s.ToString("b"));
                Assert.Fail("mi li should throw");
            }
            catch (Exception)
            {
                Assert.Pass();
            }
        }
Exemplo n.º 4
0
        public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction)
        {
            if (node.NodeType != XmlNodeType.ProcessingInstruction)
            {
                return(false);
            }

            switch (node.Name)
            {
            case "enableClientCreate":
                if (!isRoot)
                {
                    throw ParserUtils.TemplateErrorException("The enableClientCreate directive can only appear outside of the template.");
                }
                template.EnableClientCreate = true;
                return(true);

            default:
                return(false);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Parse a text that match exactly one code element.
        /// The type of the code element is compared to the parsed one.
        /// </summary>
        /// <param name="compilationUnit"></param>
        /// <param name="textToParse"></param>
        /// <param name="correctSyntax"></param>
        public static Tuple <CodeElementsDocument, T> ParseOneCodeElement <T>(string textToParse, bool correctSyntax = true) where T : CodeElement
        {
            CompilationUnit compilationUnit = ParserUtils.ParseCobolString(textToParse);

            CodeElementsDocument codeElementsDocument = compilationUnit.CodeElementsDocumentSnapshot;

            Assert.IsTrue(codeElementsDocument.CodeElements.Any());
            var firstCodeElement = codeElementsDocument.CodeElements.First();

            Assert.IsTrue(firstCodeElement.GetType() == typeof(T));

            bool codeElementOk         = firstCodeElement.Diagnostics == null || !firstCodeElement.Diagnostics.Any();
            bool codeElementDocumentOk = codeElementsDocument.ParserDiagnostics == null || !codeElementsDocument.ParserDiagnostics.Any();

            //Do not test compilationUnit.ProgramClassDocumentSnapshot.Diagnostics here because we are just parsing a single line
            //the semantic phase will produce errors.

            Assert.IsTrue((codeElementOk && codeElementDocumentOk) == correctSyntax);

            return(new Tuple <CodeElementsDocument, T>(codeElementsDocument, (T)codeElementsDocument.CodeElements.First()));
        }
Exemplo n.º 6
0
        public void GenerateObjectAndStringifyParseGloss()
        {
            //Important! create teh dialect only once, mixing this is bad biz.
            Dialect dialect = Dialect.LooseyGoosey;

            dialect.IncludeApocrypha = false;

            List <Sentence> sentences = new List <Sentence>();

            TextGenerator tg = new TextGenerator(dialect);

            for (int i = 0; i < 1000; i++)
            {
                sentences.Add(tg.GenerateSentence());
            }

            ParserUtils pu = new ParserUtils(dialect);

            GlossMaker gm = new GlossMaker();

            foreach (Sentence sentence in sentences)
            {
                string s = sentence.ToString();

                NormalizeExplicit norm = new NormalizeExplicit(dialect);
                string            sn   = norm.NormalizeText(s);
                Console.WriteLine(sn);
                Console.WriteLine(sentence.ToString("b"));
                Console.WriteLine(gm.Gloss(sn, s, dialect));

                Sentence reparsed = pu.ParsedSentenceFactory(sn, s);

                string reparseString = reparsed.ToString();
                //string normalize = Normalizer.NormalizeText(reparseString, dialect);

                string normalize = norm.NormalizeText(reparseString);
                Console.WriteLine(normalize);
                Console.WriteLine(gm.Gloss(normalize, s, dialect));
            }
        }
Exemplo n.º 7
0
        public void GenerateObjectAndStringifyParse(Dialect dialect)
        {
            Dialect d = Dialect.LooseyGoosey;

            d.IncludeApocrypha = false;
            TextGenerator   tg        = new TextGenerator(d);
            List <Sentence> sentences = new List <Sentence>();

            for (int i = 0; i < 1000; i++)
            {
                sentences.Add(tg.GenerateSentence());
            }


            ParserUtils pu = new ParserUtils(Dialect.WordProcessorRules);

            foreach (Sentence sentence in sentences)
            {
                string s = sentence.ToString();
                Console.WriteLine(s);
                try
                {
                    Sentence reparsed = pu.ParsedSentenceFactory(s, s);

                    Console.WriteLine(reparsed.ToString());
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("This isn't possible in a pi chain"))
                    {
                        Console.WriteLine("Prep phrase in a subject :-(");
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 8
0
        internal static IFragment ProcessCallFragment(IDocumentProcessor docProcessor, XmlNode node)
        {
            XmlAttribute nameAttr   = (XmlAttribute)node.Attributes.GetNamedItem("name");
            XmlAttribute paramsAttr = (XmlAttribute)node.Attributes.GetNamedItem("params");

            if (nameAttr == null)
            {
                throw ParserUtils.TemplateErrorException("The <call-fragment> element must have the name attribute specified.");
            }
            string name = nameAttr.Value;

            if (!ParserUtils.IsValidUnqualifiedName(name))
            {
                throw ParserUtils.TemplateErrorException("The name " + name + " is not a valid unqualified identifier.");
            }
            if (Utils.GetNumChildNodes(node) != 0)
            {
                throw ParserUtils.TemplateErrorException("The <call-fragment> element cannot have children.");
            }

            return(new CodeExpressionFragment(name + "(" + (paramsAttr != null ? paramsAttr.Value : "") + ")"));
        }
        public void PosessiveMiInSubject()
        {
            //const string s = "kili lon ma mi li pona tawa mi.";
            const string s = "kili, lon ma mi li pona, tawa mi.";

            Dialect    dialect = Dialect.LooseyGoosey;
            Normalizer norm    = new Normalizer(dialect);

            string normalized = norm.NormalizeText(s);

            Console.WriteLine(normalized);

            ParserUtils pu = new ParserUtils(dialect);

            Sentence sentence = pu.ParsedSentenceFactory(normalized, s);

            Console.WriteLine(s);
            Console.WriteLine(sentence.ToString());
            Console.WriteLine(sentence.ToString("b"));
            Assert.IsNotNull(sentence.Subjects);
            Assert.AreEqual(s, sentence.ToString());
        }
Exemplo n.º 10
0
        public ILink OnLinkTranslation(IUnderlayingNetworkLayer unl, EndPoint ep, IFrame frame)
        {
            try
            {
                var former = LinksTree.Find(ep, unl).GetDeviceId();
                var dev    = unl.Parser.Factory(frame, former) ?? unl.Parser;

                if (ParserUtils.IsInvalidDeviceId(dev.GetDeviceId()))
                {
                    return(null);
                }

                var link = LinksTree.GetOrCreate(dev, unl, ep, former);

                if (LogCambioDeIp)
                {
                    if (dev.GetDeviceId() != former)
                    {
                        STrace.Trace(GetType().FullName, dev.GetDeviceId(), String.Format("Detectado cambio de id: Former={0} Received={1} Address={2} Payload={3}", former, dev.GetDeviceId(), frame.RemoteAddressAsString, Encoding.ASCII.GetString(frame.Payload)));
                    }

                    if (!ep.Equals(link.EndPoint))                     // el operador == siempre da falso por que solo compara la referencia y son 2 instancias distintas con los mismos datos!
                    {
                        STrace.Trace(GetType().FullName, dev.GetDeviceId(), String.Format("Detectado cambio de ip: FormerAddress={0} ReceivedAddress={1} DeviceId={2} Payload={3}", link.EndPoint, ep, dev.GetDeviceId(), Encoding.ASCII.GetString(frame.Payload)));
                    }
                }

                return(link);
            }
            catch (ObjectNotFoundException)
            {
                STrace.Debug(GetType().FullName, String.Format("Dispositivo no registrado: {0}", frame.PayloadAsString));
            }
            catch (Exception e)
            {
                STrace.Exception(GetType().FullName, e, String.Format("OnLinkTranslation: {0}", frame.PayloadAsString));
            }
            return(null);
        }
Exemplo n.º 11
0
        private void LoadXml2(XmlElement value, XmlNamespaceManager nsm, bool hasTransforms)
        {
            XmlNodeList digestMethodNodes = value.SelectNodes("ds:DigestMethod", nsm);

            if (digestMethodNodes == null || digestMethodNodes.Count == 0 || digestMethodNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod");
            }
            XmlElement digestMethodElement = digestMethodNodes[0] as XmlElement;

            _digestMethod = ElementUtils.GetAttribute(digestMethodElement, "Algorithm", NS.XmlDsigNamespaceUrl);
            if (_digestMethod == null || !ElementUtils.VerifyAttributes(digestMethodElement, "Algorithm"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod");
            }


            XmlNodeList digestValueNodes = value.SelectNodes("ds:DigestValue", nsm);

            if (digestValueNodes == null || digestValueNodes.Count == 0 || digestValueNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue");
            }
            XmlElement digestValueElement = digestValueNodes[0] as XmlElement;

            _digestValue = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(digestValueElement.InnerText));
            if (!ElementUtils.VerifyAttributes(digestValueElement, (string[])null))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue");
            }
            int expectedChildNodeCount = hasTransforms ? 3 : 2;

            if (value.SelectNodes("*").Count != expectedChildNodeCount)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference");
            }

            _cachedXml = value;
        }
Exemplo n.º 12
0
 public TypedMarkupData Parse(string registeredPrefix, bool isArray, string value, ITemplate template)
 {
     if (isArray)
     {
         StringBuilder sb = new StringBuilder();
         sb.Append("new int[] {");
         int[] ints;
         if (value.Trim() != "")
         {
             string[] split = value.Split('|');
             ints = new int[split.Length];
             for (int i = 0; i < split.Length; i++)
             {
                 if (Utils.RegexExec(split[i], Utils.IntRegex, "") == null)
                 {
                     throw ParserUtils.TemplateErrorException(ParserUtils.MakeTypedMarkupErrorMessage(registeredPrefix, isArray, value));
                 }
                 sb.Append(i > 0 ? ", " : " ");
                 ints[i] = Utils.ParseInt(split[i]);
                 sb.Append(Utils.ToStringInvariantInt(ints[i]));
             }
         }
         else
         {
             ints = new int[0];
         }
         sb.Append(" }");
         return(new TypedMarkupData(sb.ToString(), delegate() { return ints; }));
     }
     else
     {
         if (Utils.RegexExec(value, Utils.IntRegex, "") == null)
         {
             throw ParserUtils.TemplateErrorException(ParserUtils.MakeTypedMarkupErrorMessage(registeredPrefix, isArray, value));
         }
         int i = Utils.ParseInt(value);
         return(new TypedMarkupData(Utils.ToStringInvariantInt(i), delegate() { return i; }));
     }
 }
Exemplo n.º 13
0
        // we would like to be able to parse quickly. With Antlr there is supposed to be a way to parse quicker if we arent building a tree.
        // here is a starting point for exploring that.
        private async Task <int> ParseNTStreamNoTree(Stream data)
        {
            ConcurrentQueue <Node> batch = new ConcurrentQueue <Node>();

            var parser = new NTRIPLESParser(ParserUtils.makeNTRIPLESStream(data));

            //parser.TrimParseTree = true;
            parser.BuildParseTree = true;
            int lastValidPosition = 0;

            parser.AddErrorListener(new ErrorListener());
            NTRIPLESParser.TripleContext cc = null;

            for (;; cc = parser.triple())
            {
                if (cc?.exception != null
                    //&& cc.exception.GetType() != typeof(Antlr4.Runtime.InputMismatchException)
                    //&& cc.exception.GetType() != typeof(Antlr4.Runtime.NoViableAltException)
                    )
                {
                    Console.WriteLine(cc.exception.Message);
                    Console.WriteLine(
                        $"found {cc.exception.OffendingToken.Text} at Line {cc.exception.OffendingToken.Line} offset at {cc.exception.OffendingToken.StartIndex}");
                }

                if (cc != null)
                {
                    lastValidPosition = cc.Start.StartIndex;
                }
                //lastValidPosition = parser.CurrentToken.StopIndex;
                if (parser.CurrentToken.Type == TokenConstants.Eof)
                {
                    break;
                }
            }

            return(lastValidPosition);
        }
Exemplo n.º 14
0
        public override string ToStr()
        {
            switch (GetResultType())
            {
            case SimpleTypes.Boolean:
                return(GetBoolResultOut(null).ToString());

            case SimpleTypes.String:
                return(ParserUtils.ConstToStrEscape(GetStrResultOut(null).ToString()));

            case SimpleTypes.Integer:
                return(GetIntResultOut(null).ToString());

            case SimpleTypes.Float:
                return(GetFloatResultOut(null).ToStr());

            case SimpleTypes.DateTime:
                return("datetime '" + GetDateTimeResultOut(null).ToString("dd.MM.yyyy HH:mm:ss") + "'");

            case SimpleTypes.Date:
                return("date '" + GetDateTimeResultOut(null).ToString("dd.MM.yyyy") + "'");

            case SimpleTypes.Time:
                return("time '" + GetTimeResultOut(null).ToString("c") + "'");

            case SimpleTypes.Geometry:
                throw new Exception("Can not convert geometry constant to string");

            //return "_Geometry_";

            /*Geometry g = GetGeomResultOut(null);
             * if (g == null) g = new Geometry(wkbGeometryType.wkbPolygon);
             * return "GeometryFromWkbHex(" + BaseExpressionFactory.StandartCodeEscape(CustomDbDriver.BytesToStr(g.MyExportToWKB()), '\'', '\'') +","+this.GetCoordinateSystem().EpsgCode.ToString()+ ")";
             */
            default:
                throw new Exception("Unknown data type");
            }
        }
Exemplo n.º 15
0
        public static ItemData.ItemModData ReadKind2DataNode(BinaryReader reader)
        {
            var result = new ItemData.ItemModData();

            result.ItemTdbId           = reader.ReadUInt64();
            result.Header              = ReadHeaderThing(reader);
            result.UnknownString       = ParserUtils.ReadString(reader);
            result.AttachmentSlotTdbId = reader.ReadUInt64();
            var count = ParserUtils.ReadPackedLong(reader);

            result.Children = new ItemData.ItemModData[count];
            for (var i = 0; i < count; ++i)
            {
                result.Children[i] = ReadKind2DataNode(reader);
            }

            result.Unknown2 = reader.ReadUInt32();
            result.TdbId2   = reader.ReadUInt64();
            result.Unknown3 = reader.ReadUInt32();
            result.Unknown4 = reader.ReadUInt32();

            return(result);
        }