コード例 #1
0
 private Xop_list_tkn PopTil(
     Xop_ctx ctx,
     Xop_tkn_mkr tkn_mkr,
     Xop_root_tkn root,
     byte[] src,
     int src_len,
     int bgn_pos,
     int cur_pos,
     byte subLast) {
   int acs_pos = ctx.Stack_idx_find_but_stop_at_tbl(Xop_tkn_itm_.Tid_list);
   if (acs_pos == -1) return null;
   Xop_list_tkn rv =
       (Xop_list_tkn)
           ctx.Stack_pop_til(root, src, acs_pos, false, bgn_pos, cur_pos, Xop_tkn_itm_.Tid_list);
   MakeTkn_end(ctx, tkn_mkr, root, src, src_len, bgn_pos, cur_pos, rv, subLast);
   return rv;
 }
コード例 #2
0
  public int MakeTkn_bgn(
      Xop_ctx ctx,
      Xop_tkn_mkr tkn_mkr,
      Xop_root_tkn root,
      byte[] src,
      int src_len,
      int bgn_pos,
      int cur_pos) { // REF.MW: Parser|doBlockLevels
    if (bgn_pos == Xop_parser_.Doc_bgn_bos) bgn_pos = 0; // do not allow -1 pos

    // pop hdr if exists; EX: \n== a ==\n*b; \n* needs to close hdr
    int acsPos = ctx.Stack_idx_typ(Xop_tkn_itm_.Tid_hdr);
    if (acsPos != -1)
      ctx.Stack_pop_til(root, src, acsPos, true, bgn_pos, cur_pos, Xop_tkn_itm_.Tid_list);

    // close apos
    ctx.Apos().End_frame(ctx, root, src, bgn_pos, false);
    byte symByt =
        src[
            cur_pos
                - 1]; // -1 b/c symByt is byte before curByt; EX: \n*a; cur_pos is at a; want to get
    // *
    int prvSymLen = curSymLen;
    cur_pos = SymAry_fill(src, cur_pos, src_len, symByt);
    symByt =
        src[
            cur_pos
                - 1]; // NOTE: get symByt again b/c cur_pos may have changed; EX: "#*"; # may have
    // triggered list, but last symByt should be *
    if (SymAry_fill_overflow) return ctx.Lxr_make_txt_(cur_pos);
    PrvItm_compare();
    ctx.Para()
        .Process_block__bgn__nl_w_symbol(
            ctx,
            root,
            src,
            bgn_pos,
            cur_pos - 1,
            Xop_xnde_tag_
                .Tag__li); // -1 b/c cur_pos includes sym_byte; EX: \n*; pass li; should pass
    // correct tag, but for purposes of para_wkr, <li> doesn't matter
    if (prvSymMatch) {
      PopTil(ctx, tkn_mkr, root, src, src_len, bgn_pos, cur_pos, Bool_.N_byte);
      posBldr.MoveNext();
      prvSymAry = Xop_list_wkr_.MakeSymAry(curSymAry, curSymLen);
      Xop_list_tkn prvItm =
          tkn_mkr.List_bgn(bgn_pos, cur_pos, curSymAry[curSymLen - 1], curSymLen)
              .List_path_(posBldr.XtoIntAry())
              .List_uid_(listId);
      ctx.Subs_add_and_stack(root, prvItm);
      ctx.Empty_ignored_y_();
    } else {
      for (int i = prvSymLen; i > commonSymLen; i--) { // close all discontinued itms: EX: ##\n#\n
        PopTil(ctx, tkn_mkr, root, src, src_len, bgn_pos, cur_pos, Bool_.Y_byte);
        posBldr.MoveUp();
      }
      if (commonSymLen == 0 && prvSymLen != 0) { // nothing in common; reset list
        listId++;
        posBldr.Init();
      }
      if (curSymLen == commonSymLen) { // add another itm if continuing; EX: #\n#\n
        PopTil(ctx, tkn_mkr, root, src, src_len, bgn_pos, cur_pos, Bool_.N_byte);
        if ((prvSymLen - curSymLen)
                > 0 // moving up many levels; do not open new list; just MoveNext; EX: #1\n###3\n##2
            && curSymLen
                != 1) { // do not moveNext if at level 1; this has to do with strange incrementing
          // logic in posBldr at rootLvl
          posBldr.MoveNext();
        } else {
          posBldr.MoveUp();
          posBldr.MoveDown();
        }
        prvSymAry = Xop_list_wkr_.MakeSymAry(curSymAry, curSymLen);
        symByt = src[cur_pos - 1];
        Xop_list_tkn prvItm =
            tkn_mkr.List_bgn(bgn_pos, cur_pos, symByt, curSymLen)
                .List_path_(posBldr.XtoIntAry())
                .List_uid_(listId);
        ctx.Subs_add_and_stack(root, prvItm);
        ctx.Empty_ignored_y_();
      }
      for (int i = commonSymLen; i < curSymLen; i++) { // open new itms; EX: #\n##\n
        posBldr.MoveDown();
        symByt = curSymAry[i];
        prvSymAry = Xop_list_wkr_.MakeSymAry(curSymAry, curSymLen);
        Xop_list_tkn prvItm =
            tkn_mkr.List_bgn(bgn_pos, cur_pos, symByt, i + List_adp_.Base1)
                .List_path_(posBldr.XtoIntAry())
                .List_uid_(listId);
        ctx.Subs_add_and_stack(root, prvItm);
        ctx.Empty_ignored_y_();
      }
    }
    if (allDd
        && cur_pos < src_len - 2
        && src[cur_pos] == '{'
        && src[cur_pos + 1] == '|') // NOTE: if indent && next == {| then invoke table; EX: ":::{|"
    return ctx.Tblw()
          .Make_tkn_bgn(
              ctx,
              tkn_mkr,
              root,
              src,
              src_len,
              cur_pos,
              cur_pos + 2,
              false,
              Xop_tblw_wkr.Tblw_type_tb,
              Xop_tblw_wkr.Called_from_list,
              -1,
              -1); // NOTE: ws_enabled must be set to true; see test for Adinkras; Cato the Elder
    else {
      dd_chk = symByt == Xop_list_tkn_.List_itmTyp_dt;
      return cur_pos;
    }
  }
コード例 #3
0
ファイル: Xop_curly_wkr.java プロジェクト: qq98982/xowa
	public int MakeTkn_end(Xop_ctx ctx, Xop_tkn_mkr tkn_mkr, Xop_root_tkn root, byte[] src, int src_len, int lxr_bgn_pos, int lxr_cur_pos) {
		if (ctx.Cur_tkn_tid() == Xop_tkn_itm_.Tid_brack_bgn)	// WORKAROUND: ignore }} if inside lnki; EX.CM:Template:Protected; {{#switch:a|b=[[a|ja=}}]]}}
			return ctx.Lxr_make_txt_(lxr_cur_pos);
		int lxr_end_pos = Bry_finder.Find_fwd_while(src, lxr_cur_pos, src_len, Byte_ascii.Curly_end);	// NOTE: can be many consecutive }; EX: {{a|{{{1}}}}}
		int end_tkn_len = lxr_end_pos - lxr_bgn_pos;
		boolean vnt_enabled = ctx.Wiki().Lang().Vnt_mgr().Enabled();
		while (end_tkn_len > 0) {
			int acs_pos = -1, acs_len = ctx.Stack_len();
			for (int i = acs_len - 1; i > -1; i--) {		// find auto-close pos
				Xop_tkn_itm stack_tkn = ctx.Stack_get(i);
				switch (stack_tkn.Tkn_tid()) {
					case Xop_tkn_itm_.Tid_tmpl_curly_bgn:	// found curly_bgn; mark and exit
						acs_pos = i;
						i = -1;
						break;
					case Xop_tkn_itm_.Tid_brack_bgn:		// found no curly_bgn, but found brack_bgn; note that extra }} should not close any frames beyond lnki; EX:w:Template:Cite wikisource; w:John Fletcher (playwright)
						i = -1;
						break;
					case Xop_tkn_itm_.Tid_xnde:				// found xnde; ignore; handle {{template|<poem>}}</poem>}} DATE:2014-03-03
						Xop_xnde_tkn stack_xnde = (Xop_xnde_tkn)stack_tkn;
						if (stack_xnde.Tag().Xtn())
							i = -1;
						break;
				}
			}
			if (acs_pos == -1) {	// "}}+" found but no "{{+" found; warn and output literal tkn
				ctx.Msg_log().Add_itm_none(Xop_curly_log.Bgn_not_found, src, lxr_bgn_pos, lxr_end_pos);
				ctx.Subs_add(root, tkn_mkr.Txt(lxr_bgn_pos, lxr_end_pos));
				return lxr_end_pos;
			}

			Xop_curly_bgn_tkn bgn_tkn = (Xop_curly_bgn_tkn)ctx.Stack_pop_til(root, src, acs_pos, true, lxr_bgn_pos, lxr_end_pos, Xop_tkn_itm_.Tid_tmpl_curly_bgn);	// NOTE: in theory, an unclosed [[ can be on stack; for now, ignore
			int bgn_tkn_len = bgn_tkn.Src_end() - bgn_tkn.Src_bgn();
			int bgn_tkn_pos_bgn = bgn_tkn.Src_bgn();// save original pos_bgn
			boolean vnt_dash_adjust = false;
			if (vnt_enabled ) {
				int curly_bgn_dash = bgn_tkn.Src_bgn() - 1;
				if (curly_bgn_dash > -1 && src[curly_bgn_dash] == Byte_ascii.Dash) {			// "-" exists before curlies; EX: "-{{"
					int curly_end_dash = lxr_end_pos;
					if (curly_end_dash < src_len && src[curly_end_dash] == Byte_ascii.Dash) {	// "-" exists after curlies;  EX: "}}-"
						if (bgn_tkn_len > 2 && end_tkn_len > 2) {	// more than 3 curlies at bgn / end with flanking dashes; EX: "-{{{ }}}-"; NOTE: 3 is needed b/c 2 will never be reduced; EX: "-{{" will always be "-" and "{{", not "-{" and "{"
							int numeric_val = Bry_.Xto_int_or(src, bgn_tkn.Src_end(), lxr_bgn_pos, -1);
							if (	numeric_val != -1						// do not apply if numeric val; EX:"-{{{0}}}-" vs "-{{{#expr:0}}}-" sr.w:Template:Link_FA
								&&	bgn_tkn_len == 3 && end_tkn_len == 3	// exactly 3 tokens; assume param token; "-{{{" -> "-" + "{{{" x> -> "-{" + "{{"; if unbalanced (3,4 or 4,3) fall into code below
								) {
							}												// noop; PAGE:sr.w:ДНК; EX:<span id="interwiki-{{{1}}}-fa"></span> DATE:2014-07-03
							else {
								--bgn_tkn_len;		// reduce bgn curlies by 1; EX: "{{{" -> "{{"
								++bgn_tkn_pos_bgn;	// add one to bgn tkn_pos;
								--end_tkn_len;		// reduce end curlies by 1; EX: "}}}" -> "}}"
								--lxr_end_pos;		// reduce end by 1; this will "reprocess" the final "}" as a text tkn; EX: "}}}-" -> "}}" and position before "}-"
								vnt_dash_adjust = true;
							}
						}
					}
				}
			}

			int new_tkn_len = 0;
			if		(bgn_tkn_len == end_tkn_len)	// exact match; should be majority of cases
				new_tkn_len = bgn_tkn_len;
			else if (bgn_tkn_len >  end_tkn_len)	// more bgn than end; use end, and deduct bgn; EX: {{{{{1}}}|a}}
				new_tkn_len = end_tkn_len;
			else   /*bgn_tkn_len <  end_tkn_len*/	// more end than bgn; use bgn, and deduct end; EX: {{a|{{{1}}}}}
				new_tkn_len = bgn_tkn_len;

			int keep_curly_bgn = 0;
			/* NOTE: this is a semi-hack; if bgn_tkn > new_tkn, then pretend bgn_tkn fits new_tkn, give to bldr, and then adjust back later
			EX: {{{{{1}}}|a}} -> bgn_tkn_len=5,new_tkn_len=3 -> change bgn(0, 5) to bgn(2, 5)
			The "correct" way is to insert a new_bgn_tkn after cur_bgn_tkn on root, but this would have performance implications: array would have to be resized, and all subs will have to be reindexed
			NOTE: bgn curlies should also be preserved if new_tkn_len > 3; EX: {{{{{{1}}}}}}; note that bgn = end, but len > 3
			*/
			if (bgn_tkn_len > new_tkn_len || new_tkn_len > 3) {
				bgn_tkn.Tkn_ini_pos(false, bgn_tkn.Src_end() - new_tkn_len, bgn_tkn.Src_end());
				keep_curly_bgn = 1;	// preserves {{
			}
			switch (new_tkn_len) {
				case 0:			// EXC_CASE: should not happen; warn;
					ctx.Msg_log().Add_itm_none(Xop_curly_log.Bgn_len_0, src, bgn_tkn.Src_bgn(), lxr_end_pos);
					break;		
//					case 1:			// EXC_CASE: SEE:NOTE_1;
//						break;
				case 2:			// USE_CASE: make invk_tkn
					ctx.Invk().Make_tkn(ctx, root, src, lxr_bgn_pos, lxr_bgn_pos + new_tkn_len, bgn_tkn, keep_curly_bgn);
					break;
				default:		// USE_CASE: make prm_tkn; NOTE: 3 or more
					new_tkn_len = 3;	// gobble 3 at a time; EX: 6 -> 3 -> 0; EX: 7 -> 4 -> 1;
					prm_wkr.Make_tkn(ctx, tkn_mkr, root, src, src_len, lxr_bgn_pos, lxr_bgn_pos + new_tkn_len, bgn_tkn, keep_curly_bgn);
					break;
			}
			switch (bgn_tkn_len - new_tkn_len) {	// continuation of semi-hack above; some bgn still left over; adjust and throw back on stack
				case 1: // 1 tkn; convert curly to generic text tkn
					bgn_tkn.Src_end_(bgn_tkn.Src_end() - 1);	// NOTE: shorten end of bgn_tkn by 1; TEST
					ctx.Stack_add(tkn_mkr.Txt(bgn_tkn_pos_bgn, bgn_tkn.Src_end() - new_tkn_len));
					break;
				case 0:	// noop
					break;
				default:
					bgn_tkn.Tkn_ini_pos(false, bgn_tkn_pos_bgn, bgn_tkn.Src_end() - new_tkn_len);	// bgn(2, 5) -> bgn (0, 2)
					ctx.Stack_add(bgn_tkn);
					break;
			}
			if (vnt_dash_adjust) {
				Xop_tkn_itm text_tkn = root.Subs_get_or_null(root.Subs_len() - 2);	// -2 to get tkn before newly-created tmpl / prm
				if (text_tkn == null || text_tkn.Tkn_tid() != Xop_tkn_itm_.Tid_txt)
					ctx.Wiki().Appe().Usr_dlg().Warn_many("", "", "token before curly_bgn was not text tkn; src=~{0}", String_.new_u8(src, lxr_bgn_pos, lxr_end_pos));
				else
					text_tkn.Src_end_(text_tkn.Src_end() + 1);	// +1 to extend txt_tkn with dash be 1 to include curly; EX: "-" "{{{" -> "-{" "{{"
			}

			end_tkn_len -= new_tkn_len;
			lxr_bgn_pos += new_tkn_len;	// move lxr_bgn_pos along
			if (end_tkn_len == 1) {	// SEE:NOTE_1:
				ctx.Subs_add(root, tkn_mkr.Txt(lxr_bgn_pos, lxr_bgn_pos + 1));
				end_tkn_len = 0;
				++lxr_bgn_pos;
			}
		}
		return lxr_end_pos;
	}